Skip to content

Commit 9f9a72a

Browse files
authored
Disallow object prototype property access on const enums (#55424)
1 parent 418494e commit 9f9a72a

5 files changed

+172
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32123,7 +32123,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3212332123
}
3212432124
return isErrorType(apparentType) ? errorType : apparentType;
3212532125
}
32126-
prop = getPropertyOfType(apparentType, right.escapedText, /*skipObjectFunctionPropertyAugment*/ false, /*includeTypeOnlyMembers*/ node.kind === SyntaxKind.QualifiedName);
32126+
prop = getPropertyOfType(apparentType, right.escapedText, /*skipObjectFunctionPropertyAugment*/ isConstEnumObjectType(apparentType), /*includeTypeOnlyMembers*/ node.kind === SyntaxKind.QualifiedName);
3212732127
}
3212832128
// In `Foo.Bar.Baz`, 'Foo' is not referenced if 'Bar' is a const enum or a module containing only const enums.
3212932129
// `Foo` is also not referenced in `enum FooCopy { Bar = Foo.Bar }`, because the enum member value gets inlined
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
constEnumNoObjectPrototypePropertyAccess.ts(5,19): error TS2339: Property 'constructor' does not exist on type 'typeof Bebra'.
2+
constEnumNoObjectPrototypePropertyAccess.ts(6,19): error TS2339: Property 'hasOwnProperty' does not exist on type 'typeof Bebra'.
3+
constEnumNoObjectPrototypePropertyAccess.ts(7,19): error TS2339: Property 'isPrototypeOf' does not exist on type 'typeof Bebra'.
4+
constEnumNoObjectPrototypePropertyAccess.ts(8,19): error TS2339: Property 'propertyIsEnumerable' does not exist on type 'typeof Bebra'.
5+
constEnumNoObjectPrototypePropertyAccess.ts(9,19): error TS2339: Property 'toLocaleString' does not exist on type 'typeof Bebra'.
6+
constEnumNoObjectPrototypePropertyAccess.ts(10,19): error TS2339: Property 'toString' does not exist on type 'typeof Bebra'.
7+
constEnumNoObjectPrototypePropertyAccess.ts(11,19): error TS2339: Property 'valueOf' does not exist on type 'typeof Bebra'.
8+
9+
10+
==== constEnumNoObjectPrototypePropertyAccess.ts (7 errors) ====
11+
// https://github.com/microsoft/TypeScript/issues/55421
12+
13+
const enum Bebra {}
14+
15+
console.log(Bebra.constructor)
16+
~~~~~~~~~~~
17+
!!! error TS2339: Property 'constructor' does not exist on type 'typeof Bebra'.
18+
console.log(Bebra.hasOwnProperty)
19+
~~~~~~~~~~~~~~
20+
!!! error TS2339: Property 'hasOwnProperty' does not exist on type 'typeof Bebra'.
21+
console.log(Bebra.isPrototypeOf)
22+
~~~~~~~~~~~~~
23+
!!! error TS2339: Property 'isPrototypeOf' does not exist on type 'typeof Bebra'.
24+
console.log(Bebra.propertyIsEnumerable)
25+
~~~~~~~~~~~~~~~~~~~~
26+
!!! error TS2339: Property 'propertyIsEnumerable' does not exist on type 'typeof Bebra'.
27+
console.log(Bebra.toLocaleString)
28+
~~~~~~~~~~~~~~
29+
!!! error TS2339: Property 'toLocaleString' does not exist on type 'typeof Bebra'.
30+
console.log(Bebra.toString)
31+
~~~~~~~~
32+
!!! error TS2339: Property 'toString' does not exist on type 'typeof Bebra'.
33+
console.log(Bebra.valueOf)
34+
~~~~~~~
35+
!!! error TS2339: Property 'valueOf' does not exist on type 'typeof Bebra'.
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [tests/cases/conformance/constEnums/constEnumNoObjectPrototypePropertyAccess.ts] ////
2+
3+
=== constEnumNoObjectPrototypePropertyAccess.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55421
5+
6+
const enum Bebra {}
7+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
8+
9+
console.log(Bebra.constructor)
10+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
11+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
12+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
13+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
14+
15+
console.log(Bebra.hasOwnProperty)
16+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
17+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
18+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
19+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
20+
21+
console.log(Bebra.isPrototypeOf)
22+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
23+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
24+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
25+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
26+
27+
console.log(Bebra.propertyIsEnumerable)
28+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
29+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
30+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
31+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
32+
33+
console.log(Bebra.toLocaleString)
34+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
35+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
36+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
37+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
38+
39+
console.log(Bebra.toString)
40+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
41+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
42+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
43+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
44+
45+
console.log(Bebra.valueOf)
46+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
47+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
48+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
49+
>Bebra : Symbol(Bebra, Decl(constEnumNoObjectPrototypePropertyAccess.ts, 0, 0))
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//// [tests/cases/conformance/constEnums/constEnumNoObjectPrototypePropertyAccess.ts] ////
2+
3+
=== constEnumNoObjectPrototypePropertyAccess.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55421
5+
6+
const enum Bebra {}
7+
>Bebra : Bebra
8+
9+
console.log(Bebra.constructor)
10+
>console.log(Bebra.constructor) : void
11+
>console.log : (...data: any[]) => void
12+
>console : Console
13+
>log : (...data: any[]) => void
14+
>Bebra.constructor : any
15+
>Bebra : typeof Bebra
16+
>constructor : any
17+
18+
console.log(Bebra.hasOwnProperty)
19+
>console.log(Bebra.hasOwnProperty) : void
20+
>console.log : (...data: any[]) => void
21+
>console : Console
22+
>log : (...data: any[]) => void
23+
>Bebra.hasOwnProperty : any
24+
>Bebra : typeof Bebra
25+
>hasOwnProperty : any
26+
27+
console.log(Bebra.isPrototypeOf)
28+
>console.log(Bebra.isPrototypeOf) : void
29+
>console.log : (...data: any[]) => void
30+
>console : Console
31+
>log : (...data: any[]) => void
32+
>Bebra.isPrototypeOf : any
33+
>Bebra : typeof Bebra
34+
>isPrototypeOf : any
35+
36+
console.log(Bebra.propertyIsEnumerable)
37+
>console.log(Bebra.propertyIsEnumerable) : void
38+
>console.log : (...data: any[]) => void
39+
>console : Console
40+
>log : (...data: any[]) => void
41+
>Bebra.propertyIsEnumerable : any
42+
>Bebra : typeof Bebra
43+
>propertyIsEnumerable : any
44+
45+
console.log(Bebra.toLocaleString)
46+
>console.log(Bebra.toLocaleString) : void
47+
>console.log : (...data: any[]) => void
48+
>console : Console
49+
>log : (...data: any[]) => void
50+
>Bebra.toLocaleString : any
51+
>Bebra : typeof Bebra
52+
>toLocaleString : any
53+
54+
console.log(Bebra.toString)
55+
>console.log(Bebra.toString) : void
56+
>console.log : (...data: any[]) => void
57+
>console : Console
58+
>log : (...data: any[]) => void
59+
>Bebra.toString : any
60+
>Bebra : typeof Bebra
61+
>toString : any
62+
63+
console.log(Bebra.valueOf)
64+
>console.log(Bebra.valueOf) : void
65+
>console.log : (...data: any[]) => void
66+
>console : Console
67+
>log : (...data: any[]) => void
68+
>Bebra.valueOf : any
69+
>Bebra : typeof Bebra
70+
>valueOf : any
71+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/55421
5+
6+
const enum Bebra {}
7+
8+
console.log(Bebra.constructor)
9+
console.log(Bebra.hasOwnProperty)
10+
console.log(Bebra.isPrototypeOf)
11+
console.log(Bebra.propertyIsEnumerable)
12+
console.log(Bebra.toLocaleString)
13+
console.log(Bebra.toString)
14+
console.log(Bebra.valueOf)

0 commit comments

Comments
 (0)