Skip to content

Commit 062f97a

Browse files
Kingwlmhegazy
authored andcommitted
allow const enum in type query (microsoft#21083)
1 parent ef5a289 commit 062f97a

8 files changed

Lines changed: 124 additions & 11 deletions

File tree

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19474,10 +19474,11 @@ namespace ts {
1947419474
const ok =
1947519475
(node.parent.kind === SyntaxKind.PropertyAccessExpression && (<PropertyAccessExpression>node.parent).expression === node) ||
1947619476
(node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).expression === node) ||
19477-
((node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName) && isInRightSideOfImportOrExportAssignment(<Identifier>node));
19477+
((node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName) && isInRightSideOfImportOrExportAssignment(<Identifier>node) ||
19478+
(node.parent.kind === SyntaxKind.TypeQuery && (<TypeQueryNode>node.parent).exprName === node));
1947819479

1947919480
if (!ok) {
19480-
error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment);
19481+
error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query);
1948119482
}
1948219483
}
1948319484
return type;

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@
16201620
"category": "Error",
16211621
"code": 2474
16221622
},
1623-
"'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.": {
1623+
"'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.": {
16241624
"category": "Error",
16251625
"code": 2475
16261626
},
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [constEnum3.ts]
2+
const enum TestType { foo, bar }
3+
type TestTypeStr = keyof typeof TestType;
4+
5+
function f1(f: TestType) { }
6+
function f2(f: TestTypeStr) { }
7+
8+
f1(TestType.foo)
9+
f1(TestType.bar)
10+
f2('foo')
11+
f2('bar')
12+
13+
14+
//// [constEnum3.js]
15+
function f1(f) { }
16+
function f2(f) { }
17+
f1(0 /* foo */);
18+
f1(1 /* bar */);
19+
f2('foo');
20+
f2('bar');
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/conformance/constEnums/constEnum3.ts ===
2+
const enum TestType { foo, bar }
3+
>TestType : Symbol(TestType, Decl(constEnum3.ts, 0, 0))
4+
>foo : Symbol(TestType.foo, Decl(constEnum3.ts, 0, 21))
5+
>bar : Symbol(TestType.bar, Decl(constEnum3.ts, 0, 26))
6+
7+
type TestTypeStr = keyof typeof TestType;
8+
>TestTypeStr : Symbol(TestTypeStr, Decl(constEnum3.ts, 0, 32))
9+
>TestType : Symbol(TestType, Decl(constEnum3.ts, 0, 0))
10+
11+
function f1(f: TestType) { }
12+
>f1 : Symbol(f1, Decl(constEnum3.ts, 1, 41))
13+
>f : Symbol(f, Decl(constEnum3.ts, 3, 12))
14+
>TestType : Symbol(TestType, Decl(constEnum3.ts, 0, 0))
15+
16+
function f2(f: TestTypeStr) { }
17+
>f2 : Symbol(f2, Decl(constEnum3.ts, 3, 28))
18+
>f : Symbol(f, Decl(constEnum3.ts, 4, 12))
19+
>TestTypeStr : Symbol(TestTypeStr, Decl(constEnum3.ts, 0, 32))
20+
21+
f1(TestType.foo)
22+
>f1 : Symbol(f1, Decl(constEnum3.ts, 1, 41))
23+
>TestType.foo : Symbol(TestType.foo, Decl(constEnum3.ts, 0, 21))
24+
>TestType : Symbol(TestType, Decl(constEnum3.ts, 0, 0))
25+
>foo : Symbol(TestType.foo, Decl(constEnum3.ts, 0, 21))
26+
27+
f1(TestType.bar)
28+
>f1 : Symbol(f1, Decl(constEnum3.ts, 1, 41))
29+
>TestType.bar : Symbol(TestType.bar, Decl(constEnum3.ts, 0, 26))
30+
>TestType : Symbol(TestType, Decl(constEnum3.ts, 0, 0))
31+
>bar : Symbol(TestType.bar, Decl(constEnum3.ts, 0, 26))
32+
33+
f2('foo')
34+
>f2 : Symbol(f2, Decl(constEnum3.ts, 3, 28))
35+
36+
f2('bar')
37+
>f2 : Symbol(f2, Decl(constEnum3.ts, 3, 28))
38+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=== tests/cases/conformance/constEnums/constEnum3.ts ===
2+
const enum TestType { foo, bar }
3+
>TestType : TestType
4+
>foo : TestType.foo
5+
>bar : TestType.bar
6+
7+
type TestTypeStr = keyof typeof TestType;
8+
>TestTypeStr : "foo" | "bar"
9+
>TestType : typeof TestType
10+
11+
function f1(f: TestType) { }
12+
>f1 : (f: TestType) => void
13+
>f : TestType
14+
>TestType : TestType
15+
16+
function f2(f: TestTypeStr) { }
17+
>f2 : (f: "foo" | "bar") => void
18+
>f : "foo" | "bar"
19+
>TestTypeStr : "foo" | "bar"
20+
21+
f1(TestType.foo)
22+
>f1(TestType.foo) : void
23+
>f1 : (f: TestType) => void
24+
>TestType.foo : TestType.foo
25+
>TestType : typeof TestType
26+
>foo : TestType.foo
27+
28+
f1(TestType.bar)
29+
>f1(TestType.bar) : void
30+
>f1 : (f: TestType) => void
31+
>TestType.bar : TestType.bar
32+
>TestType : typeof TestType
33+
>bar : TestType.bar
34+
35+
f2('foo')
36+
>f2('foo') : void
37+
>f2 : (f: "foo" | "bar") => void
38+
>'foo' : "foo"
39+
40+
f2('bar')
41+
>f2('bar') : void
42+
>f2 : (f: "foo" | "bar") => void
43+
>'bar' : "bar"
44+

tests/baselines/reference/constEnumErrors.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ tests/cases/compiler/constEnumErrors.ts(14,9): error TS2474: In 'const' enum dec
55
tests/cases/compiler/constEnumErrors.ts(15,10): error TS2474: In 'const' enum declarations member initializer must be constant expression.
66
tests/cases/compiler/constEnumErrors.ts(22,13): error TS2476: A const enum member can only be accessed using a string literal.
77
tests/cases/compiler/constEnumErrors.ts(24,13): error TS2476: A const enum member can only be accessed using a string literal.
8-
tests/cases/compiler/constEnumErrors.ts(26,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
9-
tests/cases/compiler/constEnumErrors.ts(27,10): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
10-
tests/cases/compiler/constEnumErrors.ts(32,5): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
8+
tests/cases/compiler/constEnumErrors.ts(26,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
9+
tests/cases/compiler/constEnumErrors.ts(27,10): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
10+
tests/cases/compiler/constEnumErrors.ts(32,5): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
1111
tests/cases/compiler/constEnumErrors.ts(40,9): error TS2477: 'const' enum member initializer was evaluated to a non-finite value.
1212
tests/cases/compiler/constEnumErrors.ts(41,9): error TS2477: 'const' enum member initializer was evaluated to a non-finite value.
1313
tests/cases/compiler/constEnumErrors.ts(42,9): error TS2478: 'const' enum member initializer was evaluated to disallowed value 'NaN'.
@@ -55,17 +55,17 @@ tests/cases/compiler/constEnumErrors.ts(42,9): error TS2478: 'const' enum member
5555

5656
var x = E2;
5757
~~
58-
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
58+
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
5959
var y = [E2];
6060
~~
61-
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
61+
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
6262

6363
function foo(t: any): void {
6464
}
6565

6666
foo(E2);
6767
~~
68-
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
68+
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
6969

7070
const enum NaNOrInfinity {
7171
A = 9007199254740992,

tests/baselines/reference/constEnumPropertyAccess2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(13,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
1+
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(13,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
22
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(14,12): error TS2476: A const enum member can only be accessed using a string literal.
33
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(16,1): error TS2322: Type '"string"' is not assignable to type 'G'.
44
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(18,3): error TS2540: Cannot assign to 'B' because it is a constant or a read-only property.
@@ -19,7 +19,7 @@ tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(18,3): error TS25
1919
// Error from referring constant enum in any other context than a property access
2020
var z = G;
2121
~
22-
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
22+
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
2323
var z1 = G[G.A];
2424
~~~
2525
!!! error TS2476: A const enum member can only be accessed using a string literal.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const enum TestType { foo, bar }
2+
type TestTypeStr = keyof typeof TestType;
3+
4+
function f1(f: TestType) { }
5+
function f2(f: TestTypeStr) { }
6+
7+
f1(TestType.foo)
8+
f1(TestType.bar)
9+
f2('foo')
10+
f2('bar')

0 commit comments

Comments
 (0)