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

Make enum literal type identical to the union of its values #60417

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21573,6 +21573,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const t = isFreshLiteralType(type) ? (type as FreshableType).regularType :
isGenericTupleType(type) ? getNormalizedTupleType(type, writing) :
getObjectFlags(type) & ObjectFlags.Reference ? (type as TypeReference).node ? createTypeReference((type as TypeReference).target, getTypeArguments(type as TypeReference)) : getSingleBaseForNonAugmentingSubtype(type) || type :
type.flags & TypeFlags.EnumLiteral && type.flags & TypeFlags.Union ? getUnionType((type as UnionType).types) :
MichaelMitchell-at marked this conversation as resolved.
Show resolved Hide resolved
type.flags & TypeFlags.UnionOrIntersection ? getNormalizedUnionOrIntersectionType(type as UnionOrIntersectionType, writing) :
type.flags & TypeFlags.Substitution ? writing ? (type as SubstitutionType).baseType : getSubstitutionIntersection(type as SubstitutionType) :
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
Expand Down
40 changes: 24 additions & 16 deletions tests/baselines/reference/enumAssignmentCompat3.errors.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
enumAssignmentCompat3.ts(68,1): error TS2322: Type 'Abcd.E' is not assignable to type 'First.E'.
Property 'd' is missing in type 'First.E'.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(70,1): error TS2322: Type 'Cd.E' is not assignable to type 'First.E'.
Each declaration of 'E.c' differs in its value, where '2' was expected but '0' was given.
Type 'E.c' is not assignable to type 'E'.
enumAssignmentCompat3.ts(71,1): error TS2322: Type 'Nope' is not assignable to type 'E'.
Type 'Nope.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(72,1): error TS2322: Type 'Decl.E' is not assignable to type 'First.E'.
Each declaration of 'E.c' differs in its value, where '2' was expected but '3' was given.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(75,1): error TS2322: Type 'First.E' is not assignable to type 'Ab.E'.
Property 'c' is missing in type 'Ab.E'.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(76,1): error TS2322: Type 'First.E' is not assignable to type 'Cd.E'.
Property 'a' is missing in type 'Cd.E'.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(77,1): error TS2322: Type 'E' is not assignable to type 'Nope'.
Type 'E.a' is not assignable to type 'Nope'.
enumAssignmentCompat3.ts(78,1): error TS2322: Type 'First.E' is not assignable to type 'Decl.E'.
Each declaration of 'E.c' differs in its value, where '3' was expected but '2' was given.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(82,1): error TS2322: Type 'Const.E' is not assignable to type 'First.E'.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(83,1): error TS2322: Type 'First.E' is not assignable to type 'Const.E'.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(86,1): error TS2322: Type 'Merged.E' is not assignable to type 'First.E'.
Each declaration of 'E.c' differs in its value, where '2' was expected but '3' was given.
Type 'E.a' is not assignable to type 'E'.
enumAssignmentCompat3.ts(87,1): error TS2322: Type 'First.E' is not assignable to type 'Merged.E'.
Each declaration of 'E.c' differs in its value, where '3' was expected but '2' was given.
Type 'E.a' is not assignable to type 'E'.


==== enumAssignmentCompat3.ts (12 errors) ====
Expand Down Expand Up @@ -91,54 +95,58 @@ enumAssignmentCompat3.ts(87,1): error TS2322: Type 'First.E' is not assignable t
abc = secondAbcd; // missing 'd'
~~~
!!! error TS2322: Type 'Abcd.E' is not assignable to type 'First.E'.
!!! error TS2322: Property 'd' is missing in type 'First.E'.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.
MichaelMitchell-at marked this conversation as resolved.
Show resolved Hide resolved
abc = secondAb; // ok
abc = secondCd; // missing 'd'
~~~
!!! error TS2322: Type 'Cd.E' is not assignable to type 'First.E'.
!!! error TS2322: Each declaration of 'E.c' differs in its value, where '2' was expected but '0' was given.
!!! error TS2322: Type 'E.c' is not assignable to type 'E'.
abc = nope; // nope!
~~~
!!! error TS2322: Type 'Nope' is not assignable to type 'E'.
!!! error TS2322: Type 'Nope.a' is not assignable to type 'E'.
abc = decl; // bad - value of 'c' differs between these enums
~~~
!!! error TS2322: Type 'Decl.E' is not assignable to type 'First.E'.
!!! error TS2322: Each declaration of 'E.c' differs in its value, where '2' was expected but '3' was given.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.
secondAbc = abc; // ok
secondAbcd = abc; // ok
secondAb = abc; // missing 'c'
~~~~~~~~
!!! error TS2322: Type 'First.E' is not assignable to type 'Ab.E'.
!!! error TS2322: Property 'c' is missing in type 'Ab.E'.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.
secondCd = abc; // missing 'a' and 'b'
~~~~~~~~
!!! error TS2322: Type 'First.E' is not assignable to type 'Cd.E'.
!!! error TS2322: Property 'a' is missing in type 'Cd.E'.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.
nope = abc; // nope!
~~~~
!!! error TS2322: Type 'E' is not assignable to type 'Nope'.
!!! error TS2322: Type 'E.a' is not assignable to type 'Nope'.
decl = abc; // bad - value of 'c' differs between these enums
~~~~
!!! error TS2322: Type 'First.E' is not assignable to type 'Decl.E'.
!!! error TS2322: Each declaration of 'E.c' differs in its value, where '3' was expected but '2' was given.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.

// const is only assignable to itself
k = k;
abc = k; // error
~~~
!!! error TS2322: Type 'Const.E' is not assignable to type 'First.E'.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.
k = abc;
~
!!! error TS2322: Type 'First.E' is not assignable to type 'Const.E'.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.

// merged enums compare all their members
abc = merged; // missing 'd'
~~~
!!! error TS2322: Type 'Merged.E' is not assignable to type 'First.E'.
!!! error TS2322: Each declaration of 'E.c' differs in its value, where '2' was expected but '3' was given.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.
merged = abc; // bad - value of 'c' differs between these enums
~~~~~~
!!! error TS2322: Type 'First.E' is not assignable to type 'Merged.E'.
!!! error TS2322: Each declaration of 'E.c' differs in its value, where '3' was expected but '2' was given.
!!! error TS2322: Type 'E.a' is not assignable to type 'E'.
abc = merged2; // ok
merged2 = abc; // ok
28 changes: 16 additions & 12 deletions tests/baselines/reference/enumAssignmentCompat6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
a.ts(36,5): error TS2322: Type 'strings.DiagnosticCategory' is not assignable to type 'numerics.DiagnosticCategory'.
Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '0' was expected but '"Warning"' was given.
Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
a.ts(37,5): error TS2322: Type 'numerics.DiagnosticCategory' is not assignable to type 'strings.DiagnosticCategory'.
Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '"Warning"' was expected but '0' was given.
Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
a.ts(41,5): error TS2322: Type 'DiagnosticCategory' is not assignable to type 'DiagnosticCategory2'.
Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory2'.
a.ts(42,5): error TS2322: Type 'DiagnosticCategory2' is not assignable to type 'DiagnosticCategory'.
Type 'DiagnosticCategory2.Warning' is not assignable to type 'DiagnosticCategory'.
a.ts(51,5): error TS2322: Type 'ambients.DiagnosticCategory' is not assignable to type 'strings.DiagnosticCategory'.
One value of 'DiagnosticCategory.Warning' is the string '"Warning"', and the other is assumed to be an unknown numeric value.
Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
a.ts(52,5): error TS2322: Type 'strings.DiagnosticCategory' is not assignable to type 'ambients.DiagnosticCategory'.
One value of 'DiagnosticCategory.Warning' is the string '"Warning"', and the other is assumed to be an unknown numeric value.
Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
f.ts(18,9): error TS2322: Type 'DiagnosticCategory' is not assignable to type 'import("f").DiagnosticCategory'.
Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '0' was expected but '"Warning"' was given.
Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
f.ts(19,9): error TS2322: Type 'import("f").DiagnosticCategory' is not assignable to type 'DiagnosticCategory'.
Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '"Warning"' was expected but '0' was given.
Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.


==== a.ts (6 errors) ====
Expand Down Expand Up @@ -53,20 +55,22 @@ f.ts(19,9): error TS2322: Type 'import("f").DiagnosticCategory' is not assignabl
x = y;
~
!!! error TS2322: Type 'strings.DiagnosticCategory' is not assignable to type 'numerics.DiagnosticCategory'.
!!! error TS2322: Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '0' was expected but '"Warning"' was given.
!!! error TS2322: Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
y = x;
~
!!! error TS2322: Type 'numerics.DiagnosticCategory' is not assignable to type 'strings.DiagnosticCategory'.
!!! error TS2322: Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '"Warning"' was expected but '0' was given.
!!! error TS2322: Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
}

function g(x: numerics.DiagnosticCategory2, y: strings.DiagnosticCategory) {
x = y;
~
!!! error TS2322: Type 'DiagnosticCategory' is not assignable to type 'DiagnosticCategory2'.
!!! error TS2322: Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory2'.
y = x;
~
!!! error TS2322: Type 'DiagnosticCategory2' is not assignable to type 'DiagnosticCategory'.
!!! error TS2322: Type 'DiagnosticCategory2.Warning' is not assignable to type 'DiagnosticCategory'.
}

function h(x: numerics.DiagnosticCategory, y: ambients.DiagnosticCategory) {
Expand All @@ -78,11 +82,11 @@ f.ts(19,9): error TS2322: Type 'import("f").DiagnosticCategory' is not assignabl
x = y;
~
!!! error TS2322: Type 'ambients.DiagnosticCategory' is not assignable to type 'strings.DiagnosticCategory'.
!!! error TS2322: One value of 'DiagnosticCategory.Warning' is the string '"Warning"', and the other is assumed to be an unknown numeric value.
!!! error TS2322: Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
y = x;
~
!!! error TS2322: Type 'strings.DiagnosticCategory' is not assignable to type 'ambients.DiagnosticCategory'.
!!! error TS2322: One value of 'DiagnosticCategory.Warning' is the string '"Warning"', and the other is assumed to be an unknown numeric value.
!!! error TS2322: Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
}

==== f.ts (2 errors) ====
Expand All @@ -106,10 +110,10 @@ f.ts(19,9): error TS2322: Type 'import("f").DiagnosticCategory' is not assignabl
x = y;
~
!!! error TS2322: Type 'DiagnosticCategory' is not assignable to type 'import("f").DiagnosticCategory'.
!!! error TS2322: Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '0' was expected but '"Warning"' was given.
!!! error TS2322: Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
y = x;
~
!!! error TS2322: Type 'import("f").DiagnosticCategory' is not assignable to type 'DiagnosticCategory'.
!!! error TS2322: Each declaration of 'DiagnosticCategory.Warning' differs in its value, where '"Warning"' was expected but '0' was given.
!!! error TS2322: Type 'DiagnosticCategory.Warning' is not assignable to type 'DiagnosticCategory'.
}
})()
4 changes: 4 additions & 0 deletions tests/baselines/reference/enumLiteralTypes3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ enumLiteralTypes3.ts(10,5): error TS2322: Type 'YesNo' is not assignable to type
enumLiteralTypes3.ts(11,5): error TS2322: Type 'UnknownYesNo' is not assignable to type 'Choice.Yes'.
Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'.
enumLiteralTypes3.ts(12,5): error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'.
Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'.
enumLiteralTypes3.ts(18,5): error TS2322: Type 'UnknownYesNo' is not assignable to type 'YesNo'.
Type 'Choice.Unknown' is not assignable to type 'YesNo'.
enumLiteralTypes3.ts(19,5): error TS2322: Type 'Choice' is not assignable to type 'YesNo'.
Type 'Choice.Unknown' is not assignable to type 'YesNo'.
enumLiteralTypes3.ts(37,5): error TS2322: Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'.
enumLiteralTypes3.ts(39,5): error TS2322: Type 'Choice.No' is not assignable to type 'Choice.Yes'.
enumLiteralTypes3.ts(40,5): error TS2322: Type 'Choice.Unknown' is not assignable to type 'YesNo'.
Expand Down Expand Up @@ -38,6 +40,7 @@ enumLiteralTypes3.ts(96,14): error TS2678: Type 'Choice.Unknown' is not comparab
a = d;
~
!!! error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'.
!!! error TS2322: Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'.
}

function f2(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
Expand All @@ -50,6 +53,7 @@ enumLiteralTypes3.ts(96,14): error TS2678: Type 'Choice.Unknown' is not comparab
b = d;
~
!!! error TS2322: Type 'Choice' is not assignable to type 'YesNo'.
!!! error TS2322: Type 'Choice.Unknown' is not assignable to type 'YesNo'.
}

function f3(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/identityRelationEnumTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/compiler/identityRelationEnumTypes.ts] ////

//// [identityRelationEnumTypes.ts]
namespace identityRelationEnumTypes {
export type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false;

export enum Enum {
A = 'a',
B = 'b',
}

export type EnumValues = typeof Enum[keyof typeof Enum];

type Result = identityRelationEnumTypes.Equals<
identityRelationEnumTypes.Enum,
identityRelationEnumTypes.EnumValues
>; // true
}


//// [identityRelationEnumTypes.js]
"use strict";
var identityRelationEnumTypes;
(function (identityRelationEnumTypes) {
var Enum;
(function (Enum) {
Enum["A"] = "a";
Enum["B"] = "b";
})(Enum = identityRelationEnumTypes.Enum || (identityRelationEnumTypes.Enum = {}));
})(identityRelationEnumTypes || (identityRelationEnumTypes = {}));


//// [identityRelationEnumTypes.d.ts]
declare namespace identityRelationEnumTypes {
type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false;
enum Enum {
A = "a",
B = "b"
}
type EnumValues = typeof Enum[keyof typeof Enum];
}
48 changes: 48 additions & 0 deletions tests/baselines/reference/identityRelationEnumTypes.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [tests/cases/compiler/identityRelationEnumTypes.ts] ////

=== identityRelationEnumTypes.ts ===
namespace identityRelationEnumTypes {
>identityRelationEnumTypes : Symbol(identityRelationEnumTypes, Decl(identityRelationEnumTypes.ts, 0, 0))

export type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false;
>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37))
>A : Symbol(A, Decl(identityRelationEnumTypes.ts, 1, 23))
>B : Symbol(B, Decl(identityRelationEnumTypes.ts, 1, 25))
>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 33))
>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 33))
>B : Symbol(B, Decl(identityRelationEnumTypes.ts, 1, 25))
>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 72))
>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 72))
>A : Symbol(A, Decl(identityRelationEnumTypes.ts, 1, 23))

export enum Enum {
>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 116))

A = 'a',
>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 22))

B = 'b',
>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16))
}

export type EnumValues = typeof Enum[keyof typeof Enum];
>EnumValues : Symbol(EnumValues, Decl(identityRelationEnumTypes.ts, 6, 5))
>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 116))
>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 116))

type Result = identityRelationEnumTypes.Equals<
>Result : Symbol(Result, Decl(identityRelationEnumTypes.ts, 8, 60))
>identityRelationEnumTypes : Symbol(identityRelationEnumTypes, Decl(identityRelationEnumTypes.ts, 0, 0))
>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37))

identityRelationEnumTypes.Enum,
>identityRelationEnumTypes : Symbol(identityRelationEnumTypes, Decl(identityRelationEnumTypes.ts, 0, 0))
>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 116))

identityRelationEnumTypes.EnumValues
>identityRelationEnumTypes : Symbol(identityRelationEnumTypes, Decl(identityRelationEnumTypes.ts, 0, 0))
>EnumValues : Symbol(EnumValues, Decl(identityRelationEnumTypes.ts, 6, 5))

>; // true
}

57 changes: 57 additions & 0 deletions tests/baselines/reference/identityRelationEnumTypes.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//// [tests/cases/compiler/identityRelationEnumTypes.ts] ////

=== identityRelationEnumTypes.ts ===
namespace identityRelationEnumTypes {
>identityRelationEnumTypes : typeof identityRelationEnumTypes
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

export type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false;
>Equals : Equals<A, B>
> : ^^^^^^^^^^^^
>true : true
> : ^^^^
>false : false
> : ^^^^^

export enum Enum {
>Enum : Enum
> : ^^^^

A = 'a',
>A : Enum.A
> : ^^^^^^
>'a' : "a"
> : ^^^

B = 'b',
>B : Enum.B
> : ^^^^^^
>'b' : "b"
> : ^^^
}

export type EnumValues = typeof Enum[keyof typeof Enum];
>EnumValues : EnumValues
> : ^^^^^^^^^^
>Enum : typeof Enum
> : ^^^^^^^^^^^
>Enum : typeof Enum
> : ^^^^^^^^^^^

type Result = identityRelationEnumTypes.Equals<
>Result : true
> : ^^^^
>identityRelationEnumTypes : any
> : ^^^

identityRelationEnumTypes.Enum,
>identityRelationEnumTypes : any
> : ^^^

identityRelationEnumTypes.EnumValues
>identityRelationEnumTypes : any
> : ^^^

>; // true
}

Loading