Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum unification and improvements #50528

Merged
merged 14 commits into from
Nov 3, 2022
Merged
406 changes: 174 additions & 232 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@
"category": "Error",
"code": 2473
},
"const enum member initializers can only contain literal values and other computed enum values.": {
"const enum member initializers must be constant expressions.": {
"category": "Error",
"code": 2474
},
Expand Down Expand Up @@ -2480,10 +2480,6 @@
"category": "Error",
"code": 2534
},
"Enum type '{0}' has members with initializers that are not literals.": {
"category": "Error",
"code": 2535
},
"Type '{0}' cannot be used to index type '{1}'.": {
"category": "Error",
"code": 2536
Expand Down Expand Up @@ -7457,7 +7453,7 @@
"category": "Error",
"code": 18032
},
"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.": {
"Type '{0}' is not assignable to type '{1}' as required for computed enum member values.": {
"category": "Error",
"code": 18033
},
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3922,8 +3922,8 @@ namespace ts {
// is distinguished from a regular type by a flags value of zero. Incomplete type
// objects are internal to the getFlowTypeOfReference function and never escape it.
export interface IncompleteType {
flags: TypeFlags; // No flags set
type: Type; // The type marked incomplete
flags: TypeFlags | 0; // No flags set
sandersn marked this conversation as resolved.
Show resolved Hide resolved
type: Type; // The type marked incomplete
}

export interface AmdDependency {
Expand Down Expand Up @@ -5583,7 +5583,7 @@ namespace ts {
String = 1 << 2,
Number = 1 << 3,
Boolean = 1 << 4,
Enum = 1 << 5,
Enum = 1 << 5, // Numeric computed enum member value
BigInt = 1 << 6,
StringLiteral = 1 << 7,
NumberLiteral = 1 << 8,
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/ambientDeclarations.types
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,35 @@ declare enum E1 {
>E1 : E1

x,
>x : E1
>x : E1.x

y,
>y : E1
>y : E1.y

z
>z : E1
>z : E1.z
}

// Ambient enum with integer literal initializer
declare enum E2 {
>E2 : E2

q,
>q : E2
>q : E2.q

a = 1,
>a : E2
>a : E2.a
>1 : 1

b,
>b : E2
>b : E2.b

c = 2,
>c : E2
>c : E2.c
>2 : 2

d
>d : E2
>d : E2.d
}

// Ambient enum members are always exported with or without export keyword
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/ambientEnumDeclaration1.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ declare enum E {
>E : E

a = 10,
>a : E
>a : E.a
>10 : 10

b = 10 + 1,
>b : E
>b : E.b
>10 + 1 : number
>10 : 10
>1 : 1

c = b,
>c : E
>b : E
>c : E.b
>b : E.b

d = (c) + 1,
>d : E
>d : E.d
>(c) + 1 : number
>(c) : E
>c : E
>(c) : E.b
>c : E.b
>1 : 1

e = 10 << 2 * 8,
>e : E
>e : E.e
>10 << 2 * 8 : number
>10 : 10
>2 * 8 : number
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/ambientEnumDeclaration2.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ declare enum E {
>E : E

a, // E.a
>a : E
>a : E.a

b, // E.b
>b : E
>b : E.b
}

declare const enum E1 {
>E1 : E1

a, // E.a = 0
>a : E1
>a : E1.a

b, // E.b = 1
>b : E1
>b : E1.b
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,7 @@ declare namespace ts {
}
export type FlowType = Type | IncompleteType;
export interface IncompleteType {
flags: TypeFlags;
flags: TypeFlags | 0;
type: Type;
}
export interface AmdDependency {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,7 @@ declare namespace ts {
}
export type FlowType = Type | IncompleteType;
export interface IncompleteType {
flags: TypeFlags;
flags: TypeFlags | 0;
type: Type;
}
export interface AmdDependency {
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/arrowFunctionContexts.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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'.
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.
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.
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(31,16): error TS2332: 'this' cannot be referenced in current location.
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'.
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.
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.
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(72,20): error TS2332: 'this' cannot be referenced in current location.


Expand Down Expand Up @@ -40,7 +40,7 @@ tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(72,20): e
enum E {
x = () => 4, // Error expected
~~~~~~~
!!! 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.
!!! error TS18033: Type '() => number' is not assignable to type 'number' as required for computed enum member values.
y = (() => this).length // error, can't use this in enum
~~~~
!!! error TS2332: 'this' cannot be referenced in current location.
Expand Down Expand Up @@ -87,7 +87,7 @@ tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(72,20): e
enum E {
x = () => 4, // Error expected
~~~~~~~
!!! 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.
!!! error TS18033: Type '() => number' is not assignable to type 'number' as required for computed enum member values.
y = (() => this).length
~~~~
!!! error TS2332: 'this' cannot be referenced in current location.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/arrowFunctionContexts.types
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ enum E {
>E : E

x = () => 4, // Error expected
>x : E
>x : E.x
>() => 4 : () => number
>4 : 4

y = (() => this).length // error, can't use this in enum
>y : E
>y : E.y
>(() => this).length : number
>(() => this) : () => any
>() => this : () => any
Expand Down Expand Up @@ -171,12 +171,12 @@ module M2 {
>E : E

x = () => 4, // Error expected
>x : E
>x : E.x
>() => 4 : () => number
>4 : 4

y = (() => this).length
>y : E
>y : E.y
>(() => this).length : number
>(() => this) : () => any
>() => this : () => any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ export declare enum require {
>require : require

_thisVal1,
>_thisVal1 : require
>_thisVal1 : require._thisVal1

_thisVal2,
>_thisVal2 : require
>_thisVal2 : require._thisVal2
}
export declare enum exports {
>exports : exports

_thisVal1,
>_thisVal1 : exports
>_thisVal1 : exports._thisVal1

_thisVal2,
>_thisVal2 : exports
>_thisVal2 : exports._thisVal2
}
declare module m1 {
>m1 : typeof m1
Expand All @@ -24,19 +24,19 @@ declare module m1 {
>require : require

_thisVal1,
>_thisVal1 : require
>_thisVal1 : require._thisVal1

_thisVal2,
>_thisVal2 : require
>_thisVal2 : require._thisVal2
}
enum exports {
>exports : exports

_thisVal1,
>_thisVal1 : exports
>_thisVal1 : exports._thisVal1

_thisVal2,
>_thisVal2 : exports
>_thisVal2 : exports._thisVal2
}
}
module m2 {
Expand All @@ -46,19 +46,19 @@ module m2 {
>require : require

_thisVal1,
>_thisVal1 : require
>_thisVal1 : require._thisVal1

_thisVal2,
>_thisVal2 : require
>_thisVal2 : require._thisVal2
}
export declare enum exports {
>exports : exports

_thisVal1,
>_thisVal1 : exports
>_thisVal1 : exports._thisVal1

_thisVal2,
>_thisVal2 : exports
>_thisVal2 : exports._thisVal2
}
}

Expand All @@ -67,19 +67,19 @@ declare enum require {
>require : require

_thisVal1,
>_thisVal1 : require
>_thisVal1 : require._thisVal1

_thisVal2,
>_thisVal2 : require
>_thisVal2 : require._thisVal2
}
declare enum exports {
>exports : exports

_thisVal1,
>_thisVal1 : exports
>_thisVal1 : exports._thisVal1

_thisVal2,
>_thisVal2 : exports
>_thisVal2 : exports._thisVal2
}
declare module m3 {
>m3 : typeof m3
Expand All @@ -88,19 +88,19 @@ declare module m3 {
>require : require

_thisVal1,
>_thisVal1 : require
>_thisVal1 : require._thisVal1

_thisVal2,
>_thisVal2 : require
>_thisVal2 : require._thisVal2
}
enum exports {
>exports : exports

_thisVal1,
>_thisVal1 : exports
>_thisVal1 : exports._thisVal1

_thisVal2,
>_thisVal2 : exports
>_thisVal2 : exports._thisVal2
}
}
module m4 {
Expand All @@ -110,18 +110,18 @@ module m4 {
>require : require

_thisVal1,
>_thisVal1 : require
>_thisVal1 : require._thisVal1

_thisVal2,
>_thisVal2 : require
>_thisVal2 : require._thisVal2
}
export declare enum exports {
>exports : exports

_thisVal1,
>_thisVal1 : exports
>_thisVal1 : exports._thisVal1

_thisVal2,
>_thisVal2 : exports
>_thisVal2 : exports._thisVal2
}
}
6 changes: 3 additions & 3 deletions tests/baselines/reference/commentOnAmbientEnum.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ declare enum C {
>C : C

a,
>a : C
>a : C.a

b,
>b : C
>b : C.b

c
>c : C
>c : C.c
}

// Don't keep this comment.
Expand Down