Skip to content

Commit 7cf842b

Browse files
authored
Enum unification and improvements (#50528)
* Fix enum classification and evaluation * References in literal enums must be to other enum members * Accept new baselines * Unify enum types + template literal constant expressions * Accept new baselines * Fix fourslash tests * Fix new compiler errors * Fix lint error * Accept new API baselines * Fix test * Better error message + consistently check enum initializers * Accept new baselines
1 parent 5a40875 commit 7cf842b

File tree

99 files changed

+1341
-1410
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1341
-1410
lines changed

src/compiler/checker.ts

Lines changed: 174 additions & 232 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,7 +2256,7 @@
22562256
"category": "Error",
22572257
"code": 2473
22582258
},
2259-
"const enum member initializers can only contain literal values and other computed enum values.": {
2259+
"const enum member initializers must be constant expressions.": {
22602260
"category": "Error",
22612261
"code": 2474
22622262
},
@@ -2480,10 +2480,6 @@
24802480
"category": "Error",
24812481
"code": 2534
24822482
},
2483-
"Enum type '{0}' has members with initializers that are not literals.": {
2484-
"category": "Error",
2485-
"code": 2535
2486-
},
24872483
"Type '{0}' cannot be used to index type '{1}'.": {
24882484
"category": "Error",
24892485
"code": 2536
@@ -7481,7 +7477,7 @@
74817477
"category": "Error",
74827478
"code": 18032
74837479
},
7484-
"Only numeric enums can have computed members, but this expression has type '{0}'. If you do not need exhaustiveness checks, consider using an object literal instead.": {
7480+
"Type '{0}' is not assignable to type '{1}' as required for computed enum member values.": {
74857481
"category": "Error",
74867482
"code": 18033
74877483
},

src/compiler/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,8 +3922,8 @@ namespace ts {
39223922
// is distinguished from a regular type by a flags value of zero. Incomplete type
39233923
// objects are internal to the getFlowTypeOfReference function and never escape it.
39243924
export interface IncompleteType {
3925-
flags: TypeFlags; // No flags set
3926-
type: Type; // The type marked incomplete
3925+
flags: TypeFlags | 0; // No flags set
3926+
type: Type; // The type marked incomplete
39273927
}
39283928

39293929
export interface AmdDependency {
@@ -5583,7 +5583,7 @@ namespace ts {
55835583
String = 1 << 2,
55845584
Number = 1 << 3,
55855585
Boolean = 1 << 4,
5586-
Enum = 1 << 5,
5586+
Enum = 1 << 5, // Numeric computed enum member value
55875587
BigInt = 1 << 6,
55885588
StringLiteral = 1 << 7,
55895589
NumberLiteral = 1 << 8,

tests/baselines/reference/ambientDeclarations.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,35 @@ declare enum E1 {
8282
>E1 : E1
8383

8484
x,
85-
>x : E1
85+
>x : E1.x
8686

8787
y,
88-
>y : E1
88+
>y : E1.y
8989

9090
z
91-
>z : E1
91+
>z : E1.z
9292
}
9393

9494
// Ambient enum with integer literal initializer
9595
declare enum E2 {
9696
>E2 : E2
9797

9898
q,
99-
>q : E2
99+
>q : E2.q
100100

101101
a = 1,
102-
>a : E2
102+
>a : E2.a
103103
>1 : 1
104104

105105
b,
106-
>b : E2
106+
>b : E2.b
107107

108108
c = 2,
109-
>c : E2
109+
>c : E2.c
110110
>2 : 2
111111

112112
d
113-
>d : E2
113+
>d : E2.d
114114
}
115115

116116
// Ambient enum members are always exported with or without export keyword

tests/baselines/reference/ambientEnumDeclaration1.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ declare enum E {
55
>E : E
66

77
a = 10,
8-
>a : E
8+
>a : E.a
99
>10 : 10
1010

1111
b = 10 + 1,
12-
>b : E
12+
>b : E.b
1313
>10 + 1 : number
1414
>10 : 10
1515
>1 : 1
1616

1717
c = b,
18-
>c : E
19-
>b : E
18+
>c : E.b
19+
>b : E.b
2020

2121
d = (c) + 1,
22-
>d : E
22+
>d : E.d
2323
>(c) + 1 : number
24-
>(c) : E
25-
>c : E
24+
>(c) : E.b
25+
>c : E.b
2626
>1 : 1
2727

2828
e = 10 << 2 * 8,
29-
>e : E
29+
>e : E.e
3030
>10 << 2 * 8 : number
3131
>10 : 10
3232
>2 * 8 : number

tests/baselines/reference/ambientEnumDeclaration2.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ declare enum E {
66
>E : E
77

88
a, // E.a
9-
>a : E
9+
>a : E.a
1010

1111
b, // E.b
12-
>b : E
12+
>b : E.b
1313
}
1414

1515
declare const enum E1 {
1616
>E1 : E1
1717

1818
a, // E.a = 0
19-
>a : E1
19+
>a : E1.a
2020

2121
b, // E.b = 1
22-
>b : E1
22+
>b : E1.b
2323
}

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ declare namespace ts {
20632063
}
20642064
export type FlowType = Type | IncompleteType;
20652065
export interface IncompleteType {
2066-
flags: TypeFlags;
2066+
flags: TypeFlags | 0;
20672067
type: Type;
20682068
}
20692069
export interface AmdDependency {

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ declare namespace ts {
20632063
}
20642064
export type FlowType = Type | IncompleteType;
20652065
export interface IncompleteType {
2066-
flags: TypeFlags;
2066+
flags: TypeFlags | 0;
20672067
type: Type;
20682068
}
20692069
export interface AmdDependency {

tests/baselines/reference/arrowFunctionContexts.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(2,1): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
2-
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(30,9): error TS18033: Only numeric enums can have computed members, but this expression has type '() => number'. If you do not need exhaustiveness checks, consider using an object literal instead.
2+
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(30,9): error TS18033: Type '() => number' is not assignable to type 'number' as required for computed enum member values.
33
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(31,16): error TS2332: 'this' cannot be referenced in current location.
44
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(43,5): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
5-
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(71,13): error TS18033: Only numeric enums can have computed members, but this expression has type '() => number'. If you do not need exhaustiveness checks, consider using an object literal instead.
5+
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(71,13): error TS18033: Type '() => number' is not assignable to type 'number' as required for computed enum member values.
66
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(72,20): error TS2332: 'this' cannot be referenced in current location.
77

88

@@ -40,7 +40,7 @@ tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(72,20): e
4040
enum E {
4141
x = () => 4, // Error expected
4242
~~~~~~~
43-
!!! error TS18033: Only numeric enums can have computed members, but this expression has type '() => number'. If you do not need exhaustiveness checks, consider using an object literal instead.
43+
!!! error TS18033: Type '() => number' is not assignable to type 'number' as required for computed enum member values.
4444
y = (() => this).length // error, can't use this in enum
4545
~~~~
4646
!!! error TS2332: 'this' cannot be referenced in current location.
@@ -87,7 +87,7 @@ tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(72,20): e
8787
enum E {
8888
x = () => 4, // Error expected
8989
~~~~~~~
90-
!!! error TS18033: Only numeric enums can have computed members, but this expression has type '() => number'. If you do not need exhaustiveness checks, consider using an object literal instead.
90+
!!! error TS18033: Type '() => number' is not assignable to type 'number' as required for computed enum member values.
9191
y = (() => this).length
9292
~~~~
9393
!!! error TS2332: 'this' cannot be referenced in current location.

tests/baselines/reference/arrowFunctionContexts.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ enum E {
6868
>E : E
6969

7070
x = () => 4, // Error expected
71-
>x : E
71+
>x : E.x
7272
>() => 4 : () => number
7373
>4 : 4
7474

7575
y = (() => this).length // error, can't use this in enum
76-
>y : E
76+
>y : E.y
7777
>(() => this).length : number
7878
>(() => this) : () => any
7979
>() => this : () => any
@@ -171,12 +171,12 @@ module M2 {
171171
>E : E
172172

173173
x = () => 4, // Error expected
174-
>x : E
174+
>x : E.x
175175
>() => 4 : () => number
176176
>4 : 4
177177

178178
y = (() => this).length
179-
>y : E
179+
>y : E.y
180180
>(() => this).length : number
181181
>(() => this) : () => any
182182
>() => this : () => any

tests/baselines/reference/collisionExportsRequireAndAmbientEnum.types

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ export declare enum require {
33
>require : require
44

55
_thisVal1,
6-
>_thisVal1 : require
6+
>_thisVal1 : require._thisVal1
77

88
_thisVal2,
9-
>_thisVal2 : require
9+
>_thisVal2 : require._thisVal2
1010
}
1111
export declare enum exports {
1212
>exports : exports
1313

1414
_thisVal1,
15-
>_thisVal1 : exports
15+
>_thisVal1 : exports._thisVal1
1616

1717
_thisVal2,
18-
>_thisVal2 : exports
18+
>_thisVal2 : exports._thisVal2
1919
}
2020
declare module m1 {
2121
>m1 : typeof m1
@@ -24,19 +24,19 @@ declare module m1 {
2424
>require : require
2525

2626
_thisVal1,
27-
>_thisVal1 : require
27+
>_thisVal1 : require._thisVal1
2828

2929
_thisVal2,
30-
>_thisVal2 : require
30+
>_thisVal2 : require._thisVal2
3131
}
3232
enum exports {
3333
>exports : exports
3434

3535
_thisVal1,
36-
>_thisVal1 : exports
36+
>_thisVal1 : exports._thisVal1
3737

3838
_thisVal2,
39-
>_thisVal2 : exports
39+
>_thisVal2 : exports._thisVal2
4040
}
4141
}
4242
module m2 {
@@ -46,19 +46,19 @@ module m2 {
4646
>require : require
4747

4848
_thisVal1,
49-
>_thisVal1 : require
49+
>_thisVal1 : require._thisVal1
5050

5151
_thisVal2,
52-
>_thisVal2 : require
52+
>_thisVal2 : require._thisVal2
5353
}
5454
export declare enum exports {
5555
>exports : exports
5656

5757
_thisVal1,
58-
>_thisVal1 : exports
58+
>_thisVal1 : exports._thisVal1
5959

6060
_thisVal2,
61-
>_thisVal2 : exports
61+
>_thisVal2 : exports._thisVal2
6262
}
6363
}
6464

@@ -67,19 +67,19 @@ declare enum require {
6767
>require : require
6868

6969
_thisVal1,
70-
>_thisVal1 : require
70+
>_thisVal1 : require._thisVal1
7171

7272
_thisVal2,
73-
>_thisVal2 : require
73+
>_thisVal2 : require._thisVal2
7474
}
7575
declare enum exports {
7676
>exports : exports
7777

7878
_thisVal1,
79-
>_thisVal1 : exports
79+
>_thisVal1 : exports._thisVal1
8080

8181
_thisVal2,
82-
>_thisVal2 : exports
82+
>_thisVal2 : exports._thisVal2
8383
}
8484
declare module m3 {
8585
>m3 : typeof m3
@@ -88,19 +88,19 @@ declare module m3 {
8888
>require : require
8989

9090
_thisVal1,
91-
>_thisVal1 : require
91+
>_thisVal1 : require._thisVal1
9292

9393
_thisVal2,
94-
>_thisVal2 : require
94+
>_thisVal2 : require._thisVal2
9595
}
9696
enum exports {
9797
>exports : exports
9898

9999
_thisVal1,
100-
>_thisVal1 : exports
100+
>_thisVal1 : exports._thisVal1
101101

102102
_thisVal2,
103-
>_thisVal2 : exports
103+
>_thisVal2 : exports._thisVal2
104104
}
105105
}
106106
module m4 {
@@ -110,18 +110,18 @@ module m4 {
110110
>require : require
111111

112112
_thisVal1,
113-
>_thisVal1 : require
113+
>_thisVal1 : require._thisVal1
114114

115115
_thisVal2,
116-
>_thisVal2 : require
116+
>_thisVal2 : require._thisVal2
117117
}
118118
export declare enum exports {
119119
>exports : exports
120120

121121
_thisVal1,
122-
>_thisVal1 : exports
122+
>_thisVal1 : exports._thisVal1
123123

124124
_thisVal2,
125-
>_thisVal2 : exports
125+
>_thisVal2 : exports._thisVal2
126126
}
127127
}

tests/baselines/reference/commentOnAmbientEnum.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ declare enum C {
1414
>C : C
1515

1616
a,
17-
>a : C
17+
>a : C.a
1818

1919
b,
20-
>b : C
20+
>b : C.b
2121

2222
c
23-
>c : C
23+
>c : C.c
2424
}
2525

2626
// Don't keep this comment.

0 commit comments

Comments
 (0)