diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a096d76c17a84..78e6ef647e100 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15146,7 +15146,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { * no constituent property has type 'never', but the intersection of the constituent property types is 'never'. */ function getReducedType(type: Type): Type { - if (type.flags & TypeFlags.Union && (type as UnionType).objectFlags & ObjectFlags.ContainsIntersections) { + if (type.flags & TypeFlags.Union && (type as UnionType).objectFlags) { return (type as UnionType).resolvedReducedType || ((type as UnionType).resolvedReducedType = getReducedUnionType(type as UnionType)); } else if (type.flags & TypeFlags.Intersection) { @@ -15161,14 +15161,32 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function getReducedUnionType(unionType: UnionType) { const reducedTypes = sameMap(unionType.types, getReducedType); - if (reducedTypes === unionType.types) { - return unionType; + + if (reducedTypes !== unionType.types) { + const reduced = getUnionType(reducedTypes); + if (reduced.flags & TypeFlags.Union) { + unionType = reduced as UnionType; + } + else { + return reduced; + } } - const reduced = getUnionType(reducedTypes); - if (reduced.flags & TypeFlags.Union) { - (reduced as UnionType).resolvedReducedType = reduced; + + let reducedType: Type = unionType; + + if (!(unionType.flags & TypeFlags.EnumLiteral) && every(unionType.types, t => !!(t.flags & TypeFlags.EnumLike))) { + const baseType = getBaseTypeOfEnumLikeType(first(unionType.types)); + if ( + baseType.flags & TypeFlags.Union && + unionType.types.length === (baseType as UnionType).types.length && + every(unionType.types, t => getBaseTypeOfEnumLikeType(t) === baseType) + ) { + reducedType = baseType; + } } - return reduced; + + unionType.resolvedReducedType = reducedType; + return reducedType; } function isNeverReducedProperty(prop: Symbol) { diff --git a/tests/baselines/reference/enumLiteralTypes1.types b/tests/baselines/reference/enumLiteralTypes1.types index 6e3d354d44a99..880a88871d09a 100644 --- a/tests/baselines/reference/enumLiteralTypes1.types +++ b/tests/baselines/reference/enumLiteralTypes1.types @@ -28,8 +28,8 @@ type NoYes = Choice.No | Choice.Yes; > : ^^^ type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; ->UnknownYesNo : UnknownYesNo -> : ^^^^^^^^^^^^ +>UnknownYesNo : Choice +> : ^^^^^^ >Choice : any > : ^^^ >Choice : any @@ -71,16 +71,16 @@ function f2(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ b = a; >b = a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >a : YesNo > : ^^^^^ @@ -346,8 +346,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ @@ -396,8 +396,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^^^^^^ >g : { (x: Choice.Yes): string; (x: Choice.No): boolean; (x: Choice): number; } > : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var z5 = g(c); >z5 : number @@ -499,12 +499,12 @@ function f11(x: YesNo) { function f12(x: UnknownYesNo) { >f12 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x) { ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ x; >x : Choice.Yes | Choice.No @@ -512,22 +512,22 @@ function f12(x: UnknownYesNo) { } else { x; ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ } } function f13(x: UnknownYesNo) { >f13 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x === Choice.Yes) { >x === Choice.Yes : boolean > : ^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice diff --git a/tests/baselines/reference/enumLiteralTypes2.types b/tests/baselines/reference/enumLiteralTypes2.types index fa51ba17cd341..0951abc47cfb8 100644 --- a/tests/baselines/reference/enumLiteralTypes2.types +++ b/tests/baselines/reference/enumLiteralTypes2.types @@ -28,8 +28,8 @@ type NoYes = Choice.No | Choice.Yes; > : ^^^ type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; ->UnknownYesNo : UnknownYesNo -> : ^^^^^^^^^^^^ +>UnknownYesNo : Choice +> : ^^^^^^ >Choice : any > : ^^^ >Choice : any @@ -71,16 +71,16 @@ function f2(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ b = a; >b = a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >a : YesNo > : ^^^^^ @@ -108,8 +108,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^^^^ >Choice : any > : ^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a + b; >x : number @@ -118,8 +118,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a - b; >x : number @@ -128,8 +128,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a * b; >x : number @@ -138,8 +138,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a / b; >x : number @@ -148,8 +148,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a % b; >x : number @@ -158,8 +158,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a | b; >x : number @@ -168,8 +168,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a & b; >x : number @@ -178,8 +178,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = a ^ b; >x : number @@ -188,24 +188,24 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = -b; >x : number > : ^^^^^^ >-b : number > : ^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var x = ~b; >x : number > : ^^^^^^ >~b : number > : ^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a == b; >y : boolean @@ -214,8 +214,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a != b; >y : boolean @@ -224,8 +224,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a === b; >y : boolean @@ -234,8 +234,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a !== b; >y : boolean @@ -244,8 +244,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a > b; >y : boolean @@ -254,8 +254,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a < b; >y : boolean @@ -264,8 +264,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a >= b; >y : boolean @@ -274,8 +274,8 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = a <= b; >y : boolean @@ -284,16 +284,16 @@ function f3(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var y = !b; >y : boolean > : ^^^^^^^ >!b : boolean > : ^^^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ } function f4(a: Choice.Yes, b: UnknownYesNo) { @@ -303,8 +303,8 @@ function f4(a: Choice.Yes, b: UnknownYesNo) { > : ^^^^^^^^^^ >Choice : any > : ^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ a++; >a++ : number @@ -346,8 +346,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ @@ -396,8 +396,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^^^^^^ >g : { (x: Choice.Yes): string; (x: Choice.No): boolean; (x: Choice): number; } > : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var z5 = g(c); >z5 : number @@ -499,12 +499,12 @@ function f11(x: YesNo) { function f12(x: UnknownYesNo) { >f12 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x) { ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ x; >x : Choice.Yes | Choice.No @@ -520,14 +520,14 @@ function f12(x: UnknownYesNo) { function f13(x: UnknownYesNo) { >f13 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x === Choice.Yes) { >x === Choice.Yes : boolean > : ^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice diff --git a/tests/baselines/reference/enumLiteralTypes3.errors.txt b/tests/baselines/reference/enumLiteralTypes3.errors.txt index 600c4b04c7c26..3c27a84218533 100644 --- a/tests/baselines/reference/enumLiteralTypes3.errors.txt +++ b/tests/baselines/reference/enumLiteralTypes3.errors.txt @@ -1,10 +1,8 @@ enumLiteralTypes3.ts(10,5): error TS2322: Type 'YesNo' is not assignable to type 'Choice.Yes'. Type 'Choice.No' is not assignable to type 'Choice.Yes'. -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(11,5): error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'. enumLiteralTypes3.ts(12,5): error TS2322: Type 'Choice' 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(18,5): error TS2322: Type 'Choice' is not assignable to type 'YesNo'. enumLiteralTypes3.ts(19,5): error TS2322: Type 'Choice' 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'. @@ -33,8 +31,7 @@ enumLiteralTypes3.ts(96,14): error TS2678: Type 'Choice.Unknown' is not comparab !!! error TS2322: Type 'Choice.No' is not assignable to type 'Choice.Yes'. a = c; ~ -!!! error TS2322: Type 'UnknownYesNo' is not assignable to type 'Choice.Yes'. -!!! error TS2322: Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'. +!!! error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'. a = d; ~ !!! error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'. @@ -45,8 +42,7 @@ enumLiteralTypes3.ts(96,14): error TS2678: Type 'Choice.Unknown' is not comparab b = b; b = c; ~ -!!! error TS2322: Type 'UnknownYesNo' is not assignable to type 'YesNo'. -!!! error TS2322: Type 'Choice.Unknown' is not assignable to type 'YesNo'. +!!! error TS2322: Type 'Choice' is not assignable to type 'YesNo'. b = d; ~ !!! error TS2322: Type 'Choice' is not assignable to type 'YesNo'. diff --git a/tests/baselines/reference/enumLiteralTypes3.types b/tests/baselines/reference/enumLiteralTypes3.types index 91d942e5fbd43..c9e82ec4d6912 100644 --- a/tests/baselines/reference/enumLiteralTypes3.types +++ b/tests/baselines/reference/enumLiteralTypes3.types @@ -34,8 +34,8 @@ type NoYes = Choice.No | Choice.Yes; > : ^^^ type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; ->UnknownYesNo : UnknownYesNo -> : ^^^^^^^^^^^^ +>UnknownYesNo : Choice +> : ^^^^^^ >Choice : any > : ^^^ >Choice : any @@ -50,8 +50,8 @@ function f1(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -72,12 +72,12 @@ function f1(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^ a = c; ->a = c : UnknownYesNo -> : ^^^^^^^^^^^^ +>a = c : Choice +> : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ a = d; >a = d : Choice @@ -95,8 +95,8 @@ function f2(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -117,12 +117,12 @@ function f2(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ b = c; ->b = c : UnknownYesNo -> : ^^^^^^^^^^^^ +>b = c : Choice +> : ^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ b = d; >b = d : Choice @@ -140,40 +140,40 @@ function f3(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ c = a; >c = a : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ c = b; >c = b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >b : YesNo > : ^^^^^ c = c; >c = c : Choice.Yes | Choice.No > : ^^^^^^^^^^^^^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >c : Choice.Yes | Choice.No > : ^^^^^^^^^^^^^^^^^^^^^^ c = d; >c = d : Choice > : ^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ } @@ -185,8 +185,8 @@ function f4(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -207,12 +207,12 @@ function f4(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^ d = c; ->d = c : UnknownYesNo -> : ^^^^^^^^^^^^ +>d = c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ d = d; >d = d : Choice @@ -230,8 +230,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -310,8 +310,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c = Choice.Unknown; >c = Choice.Unknown : Choice.Unknown > : ^^^^^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Unknown : Choice.Unknown > : ^^^^^^^^^^^^^^ >Choice : typeof Choice @@ -322,8 +322,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c = Choice.Yes; >c = Choice.Yes : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice @@ -334,8 +334,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c = Choice.No; >c = Choice.No : Choice.No > : ^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.No : Choice.No > : ^^^^^^^^^ >Choice : typeof Choice @@ -387,8 +387,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -467,8 +467,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === Choice.Unknown; >c === Choice.Unknown : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Unknown : Choice.Unknown > : ^^^^^^^^^^^^^^ >Choice : typeof Choice @@ -479,8 +479,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === Choice.Yes; >c === Choice.Yes : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice @@ -491,8 +491,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === Choice.No; >c === Choice.No : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.No : Choice.No > : ^^^^^^^^^ >Choice : typeof Choice @@ -544,8 +544,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -570,8 +570,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ a === d; >a === d : boolean @@ -602,8 +602,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ b === d; >b === d : boolean @@ -616,32 +616,32 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === a; >c === a : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ c === b; >c === b : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >b : YesNo > : ^^^^^ c === c; >c === c : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ +>c : Choice +> : ^^^^^^ c === d; >c === d : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -666,8 +666,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^ >d : Choice > : ^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ d === d; >d === d : boolean @@ -771,12 +771,12 @@ function f11(x: YesNo): YesNo { function f12(x: UnknownYesNo): UnknownYesNo { >f12 : (x: UnknownYesNo) => UnknownYesNo > : ^ ^^ ^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ switch (x) { ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ case Choice.Unknown: return x; >Choice.Unknown : Choice.Unknown diff --git a/tests/baselines/reference/identityRelationEnumTypes.js b/tests/baselines/reference/identityRelationEnumTypes.js new file mode 100644 index 0000000000000..19831c669cfba --- /dev/null +++ b/tests/baselines/reference/identityRelationEnumTypes.js @@ -0,0 +1,55 @@ +//// [tests/cases/compiler/identityRelationEnumTypes.ts] //// + +//// [identityRelationEnumTypes.ts] +namespace identityRelationEnumTypes { + type Equals = (() => T extends B ? 1 : 0) extends (() => T extends A ? 1 : 0) ? true : false; + + enum Enum { + A = 'a', + B = 'b', + } + + enum DifferentEnum { + A = 'a', + B = 'b', + } + + type Identical1 = typeof Enum[keyof typeof Enum]; + type Identical2 = Enum.A | Enum.B; + type Identical3 = Enum.B | Enum.A | Enum.B; + type Identical4 = Enum.B | (Enum.A | Enum.B); + type Identical5 = (Enum.A & {}) | (Enum.B & {}) + + type Different1 = Enum.A | Enum.A; + type Different2 = Enum.A | DifferentEnum.B; + + type Result1 = Equals; // true + type Result2 = Equals; // true + type Result3 = Equals; // true + type Result4 = Equals; // true + type Result5 = Equals; // true + type Result6 = Equals; // false + type Result7 = Equals; // false +} + + +//// [identityRelationEnumTypes.js] +"use strict"; +var identityRelationEnumTypes; +(function (identityRelationEnumTypes) { + var Enum; + (function (Enum) { + Enum["A"] = "a"; + Enum["B"] = "b"; + })(Enum || (Enum = {})); + var DifferentEnum; + (function (DifferentEnum) { + DifferentEnum["A"] = "a"; + DifferentEnum["B"] = "b"; + })(DifferentEnum || (DifferentEnum = {})); +})(identityRelationEnumTypes || (identityRelationEnumTypes = {})); + + +//// [identityRelationEnumTypes.d.ts] +declare namespace identityRelationEnumTypes { +} diff --git a/tests/baselines/reference/identityRelationEnumTypes.symbols b/tests/baselines/reference/identityRelationEnumTypes.symbols new file mode 100644 index 0000000000000..8aab54a38fcc1 --- /dev/null +++ b/tests/baselines/reference/identityRelationEnumTypes.symbols @@ -0,0 +1,131 @@ +//// [tests/cases/compiler/identityRelationEnumTypes.ts] //// + +=== identityRelationEnumTypes.ts === +namespace identityRelationEnumTypes { +>identityRelationEnumTypes : Symbol(identityRelationEnumTypes, Decl(identityRelationEnumTypes.ts, 0, 0)) + + type Equals = (() => T extends B ? 1 : 0) extends (() => T extends A ? 1 : 0) ? true : false; +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>A : Symbol(A, Decl(identityRelationEnumTypes.ts, 1, 16)) +>B : Symbol(B, Decl(identityRelationEnumTypes.ts, 1, 18)) +>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 26)) +>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 26)) +>B : Symbol(B, Decl(identityRelationEnumTypes.ts, 1, 18)) +>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 65)) +>T : Symbol(T, Decl(identityRelationEnumTypes.ts, 1, 65)) +>A : Symbol(A, Decl(identityRelationEnumTypes.ts, 1, 16)) + + enum Enum { +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) + + A = 'a', +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) + + B = 'b', +>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16)) + } + + enum DifferentEnum { +>DifferentEnum : Symbol(DifferentEnum, Decl(identityRelationEnumTypes.ts, 6, 5)) + + A = 'a', +>A : Symbol(DifferentEnum.A, Decl(identityRelationEnumTypes.ts, 8, 24)) + + B = 'b', +>B : Symbol(DifferentEnum.B, Decl(identityRelationEnumTypes.ts, 9, 16)) + } + + type Identical1 = typeof Enum[keyof typeof Enum]; +>Identical1 : Symbol(Identical1, Decl(identityRelationEnumTypes.ts, 11, 5)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) + + type Identical2 = Enum.A | Enum.B; +>Identical2 : Symbol(Identical2, Decl(identityRelationEnumTypes.ts, 13, 53)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16)) + + type Identical3 = Enum.B | Enum.A | Enum.B; +>Identical3 : Symbol(Identical3, Decl(identityRelationEnumTypes.ts, 14, 38)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16)) + + type Identical4 = Enum.B | (Enum.A | Enum.B); +>Identical4 : Symbol(Identical4, Decl(identityRelationEnumTypes.ts, 15, 47)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16)) + + type Identical5 = (Enum.A & {}) | (Enum.B & {}) +>Identical5 : Symbol(Identical5, Decl(identityRelationEnumTypes.ts, 16, 49)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>B : Symbol(Enum.B, Decl(identityRelationEnumTypes.ts, 4, 16)) + + type Different1 = Enum.A | Enum.A; +>Different1 : Symbol(Different1, Decl(identityRelationEnumTypes.ts, 17, 51)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) + + type Different2 = Enum.A | DifferentEnum.B; +>Different2 : Symbol(Different2, Decl(identityRelationEnumTypes.ts, 19, 38)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>A : Symbol(Enum.A, Decl(identityRelationEnumTypes.ts, 3, 15)) +>DifferentEnum : Symbol(DifferentEnum, Decl(identityRelationEnumTypes.ts, 6, 5)) +>B : Symbol(DifferentEnum.B, Decl(identityRelationEnumTypes.ts, 9, 16)) + + type Result1 = Equals; // true +>Result1 : Symbol(Result1, Decl(identityRelationEnumTypes.ts, 20, 47)) +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Identical1 : Symbol(Identical1, Decl(identityRelationEnumTypes.ts, 11, 5)) + + type Result2 = Equals; // true +>Result2 : Symbol(Result2, Decl(identityRelationEnumTypes.ts, 22, 44)) +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Identical2 : Symbol(Identical2, Decl(identityRelationEnumTypes.ts, 13, 53)) + + type Result3 = Equals; // true +>Result3 : Symbol(Result3, Decl(identityRelationEnumTypes.ts, 23, 44)) +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Identical3 : Symbol(Identical3, Decl(identityRelationEnumTypes.ts, 14, 38)) + + type Result4 = Equals; // true +>Result4 : Symbol(Result4, Decl(identityRelationEnumTypes.ts, 24, 44)) +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Identical4 : Symbol(Identical4, Decl(identityRelationEnumTypes.ts, 15, 47)) + + type Result5 = Equals; // true +>Result5 : Symbol(Result5, Decl(identityRelationEnumTypes.ts, 25, 44)) +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Identical5 : Symbol(Identical5, Decl(identityRelationEnumTypes.ts, 16, 49)) + + type Result6 = Equals; // false +>Result6 : Symbol(Result6, Decl(identityRelationEnumTypes.ts, 26, 44)) +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Different1 : Symbol(Different1, Decl(identityRelationEnumTypes.ts, 17, 51)) + + type Result7 = Equals; // false +>Result7 : Symbol(Result7, Decl(identityRelationEnumTypes.ts, 27, 44)) +>Equals : Symbol(Equals, Decl(identityRelationEnumTypes.ts, 0, 37)) +>Enum : Symbol(Enum, Decl(identityRelationEnumTypes.ts, 1, 109)) +>Different2 : Symbol(Different2, Decl(identityRelationEnumTypes.ts, 19, 38)) +} + diff --git a/tests/baselines/reference/identityRelationEnumTypes.types b/tests/baselines/reference/identityRelationEnumTypes.types new file mode 100644 index 0000000000000..5bbda1a043866 --- /dev/null +++ b/tests/baselines/reference/identityRelationEnumTypes.types @@ -0,0 +1,138 @@ +//// [tests/cases/compiler/identityRelationEnumTypes.ts] //// + +=== identityRelationEnumTypes.ts === +namespace identityRelationEnumTypes { +>identityRelationEnumTypes : typeof identityRelationEnumTypes +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + type Equals = (() => T extends B ? 1 : 0) extends (() => T extends A ? 1 : 0) ? true : false; +>Equals : Equals +> : ^^^^^^^^^^^^ +>true : true +> : ^^^^ +>false : false +> : ^^^^^ + + enum Enum { +>Enum : Enum +> : ^^^^ + + A = 'a', +>A : Enum.A +> : ^^^^^^ +>'a' : "a" +> : ^^^ + + B = 'b', +>B : Enum.B +> : ^^^^^^ +>'b' : "b" +> : ^^^ + } + + enum DifferentEnum { +>DifferentEnum : DifferentEnum +> : ^^^^^^^^^^^^^ + + A = 'a', +>A : DifferentEnum.A +> : ^^^^^^^^^^^^^^^ +>'a' : "a" +> : ^^^ + + B = 'b', +>B : DifferentEnum.B +> : ^^^^^^^^^^^^^^^ +>'b' : "b" +> : ^^^ + } + + type Identical1 = typeof Enum[keyof typeof Enum]; +>Identical1 : Enum +> : ^^^^ +>Enum : typeof Enum +> : ^^^^^^^^^^^ +>Enum : typeof Enum +> : ^^^^^^^^^^^ + + type Identical2 = Enum.A | Enum.B; +>Identical2 : Enum +> : ^^^^ +>Enum : any +> : ^^^ +>Enum : any +> : ^^^ + + type Identical3 = Enum.B | Enum.A | Enum.B; +>Identical3 : Enum +> : ^^^^ +>Enum : any +> : ^^^ +>Enum : any +> : ^^^ +>Enum : any +> : ^^^ + + type Identical4 = Enum.B | (Enum.A | Enum.B); +>Identical4 : Enum +> : ^^^^ +>Enum : any +> : ^^^ +>Enum : any +> : ^^^ +>Enum : any +> : ^^^ + + type Identical5 = (Enum.A & {}) | (Enum.B & {}) +>Identical5 : Enum +> : ^^^^ +>Enum : any +> : ^^^ +>Enum : any +> : ^^^ + + type Different1 = Enum.A | Enum.A; +>Different1 : Enum.A +> : ^^^^^^ +>Enum : any +> : ^^^ +>Enum : any +> : ^^^ + + type Different2 = Enum.A | DifferentEnum.B; +>Different2 : Different2 +> : ^^^^^^^^^^ +>Enum : any +> : ^^^ +>DifferentEnum : any +> : ^^^ + + type Result1 = Equals; // true +>Result1 : true +> : ^^^^ + + type Result2 = Equals; // true +>Result2 : true +> : ^^^^ + + type Result3 = Equals; // true +>Result3 : true +> : ^^^^ + + type Result4 = Equals; // true +>Result4 : true +> : ^^^^ + + type Result5 = Equals; // true +>Result5 : true +> : ^^^^ + + type Result6 = Equals; // false +>Result6 : false +> : ^^^^^ + + type Result7 = Equals; // false +>Result7 : false +> : ^^^^^ +} + diff --git a/tests/baselines/reference/stringEnumLiteralTypes1.types b/tests/baselines/reference/stringEnumLiteralTypes1.types index 740c8eed83b61..54b7f0ea8d752 100644 --- a/tests/baselines/reference/stringEnumLiteralTypes1.types +++ b/tests/baselines/reference/stringEnumLiteralTypes1.types @@ -34,8 +34,8 @@ type NoYes = Choice.No | Choice.Yes; > : ^^^ type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; ->UnknownYesNo : UnknownYesNo -> : ^^^^^^^^^^^^ +>UnknownYesNo : Choice +> : ^^^^^^ >Choice : any > : ^^^ >Choice : any @@ -77,16 +77,16 @@ function f2(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ b = a; >b = a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >a : YesNo > : ^^^^^ @@ -243,8 +243,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ @@ -293,8 +293,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^^^^^^ >g : { (x: Choice.Yes): string; (x: Choice.No): boolean; (x: Choice): number; } > : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var z5 = g(c); >z5 : number @@ -396,12 +396,12 @@ function f11(x: YesNo) { function f12(x: UnknownYesNo) { >f12 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x) { ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ x; >x : Choice.Yes | Choice.No @@ -409,22 +409,22 @@ function f12(x: UnknownYesNo) { } else { x; ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ } } function f13(x: UnknownYesNo) { >f13 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x === Choice.Yes) { >x === Choice.Yes : boolean > : ^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice diff --git a/tests/baselines/reference/stringEnumLiteralTypes2.types b/tests/baselines/reference/stringEnumLiteralTypes2.types index 7ca1f635ada86..b01f2e69711b7 100644 --- a/tests/baselines/reference/stringEnumLiteralTypes2.types +++ b/tests/baselines/reference/stringEnumLiteralTypes2.types @@ -34,8 +34,8 @@ type NoYes = Choice.No | Choice.Yes; > : ^^^ type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; ->UnknownYesNo : UnknownYesNo -> : ^^^^^^^^^^^^ +>UnknownYesNo : Choice +> : ^^^^^^ >Choice : any > : ^^^ >Choice : any @@ -77,16 +77,16 @@ function f2(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ b = a; >b = a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >a : YesNo > : ^^^^^ @@ -243,8 +243,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ >a : YesNo > : ^^^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ >c : Choice > : ^^^^^^ @@ -293,8 +293,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) { > : ^^^^^^ >g : { (x: Choice.Yes): string; (x: Choice.No): boolean; (x: Choice): number; } > : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ->b : UnknownYesNo -> : ^^^^^^^^^^^^ +>b : Choice +> : ^^^^^^ var z5 = g(c); >z5 : number @@ -396,12 +396,12 @@ function f11(x: YesNo) { function f12(x: UnknownYesNo) { >f12 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x) { ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ x; >x : Choice.Yes | Choice.No @@ -417,14 +417,14 @@ function f12(x: UnknownYesNo) { function f13(x: UnknownYesNo) { >f13 : (x: UnknownYesNo) => void > : ^ ^^ ^^^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ if (x === Choice.Yes) { >x === Choice.Yes : boolean > : ^^^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice diff --git a/tests/baselines/reference/stringEnumLiteralTypes3.errors.txt b/tests/baselines/reference/stringEnumLiteralTypes3.errors.txt index b0c7a09dc97a4..e9c5494853e86 100644 --- a/tests/baselines/reference/stringEnumLiteralTypes3.errors.txt +++ b/tests/baselines/reference/stringEnumLiteralTypes3.errors.txt @@ -1,10 +1,8 @@ stringEnumLiteralTypes3.ts(10,5): error TS2322: Type 'YesNo' is not assignable to type 'Choice.Yes'. Type 'Choice.No' is not assignable to type 'Choice.Yes'. -stringEnumLiteralTypes3.ts(11,5): error TS2322: Type 'UnknownYesNo' is not assignable to type 'Choice.Yes'. - Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'. +stringEnumLiteralTypes3.ts(11,5): error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'. stringEnumLiteralTypes3.ts(12,5): error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'. -stringEnumLiteralTypes3.ts(18,5): error TS2322: Type 'UnknownYesNo' is not assignable to type 'YesNo'. - Type 'Choice.Unknown' is not assignable to type 'YesNo'. +stringEnumLiteralTypes3.ts(18,5): error TS2322: Type 'Choice' is not assignable to type 'YesNo'. stringEnumLiteralTypes3.ts(19,5): error TS2322: Type 'Choice' is not assignable to type 'YesNo'. stringEnumLiteralTypes3.ts(37,5): error TS2322: Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'. stringEnumLiteralTypes3.ts(39,5): error TS2322: Type 'Choice.No' is not assignable to type 'Choice.Yes'. @@ -33,8 +31,7 @@ stringEnumLiteralTypes3.ts(96,14): error TS2678: Type 'Choice.Unknown' is not co !!! error TS2322: Type 'Choice.No' is not assignable to type 'Choice.Yes'. a = c; ~ -!!! error TS2322: Type 'UnknownYesNo' is not assignable to type 'Choice.Yes'. -!!! error TS2322: Type 'Choice.Unknown' is not assignable to type 'Choice.Yes'. +!!! error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'. a = d; ~ !!! error TS2322: Type 'Choice' is not assignable to type 'Choice.Yes'. @@ -45,8 +42,7 @@ stringEnumLiteralTypes3.ts(96,14): error TS2678: Type 'Choice.Unknown' is not co b = b; b = c; ~ -!!! error TS2322: Type 'UnknownYesNo' is not assignable to type 'YesNo'. -!!! error TS2322: Type 'Choice.Unknown' is not assignable to type 'YesNo'. +!!! error TS2322: Type 'Choice' is not assignable to type 'YesNo'. b = d; ~ !!! error TS2322: Type 'Choice' is not assignable to type 'YesNo'. diff --git a/tests/baselines/reference/stringEnumLiteralTypes3.types b/tests/baselines/reference/stringEnumLiteralTypes3.types index 440ab6c127961..6b4b1a43343cb 100644 --- a/tests/baselines/reference/stringEnumLiteralTypes3.types +++ b/tests/baselines/reference/stringEnumLiteralTypes3.types @@ -40,8 +40,8 @@ type NoYes = Choice.No | Choice.Yes; > : ^^^ type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; ->UnknownYesNo : UnknownYesNo -> : ^^^^^^^^^^^^ +>UnknownYesNo : Choice +> : ^^^^^^ >Choice : any > : ^^^ >Choice : any @@ -56,8 +56,8 @@ function f1(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -78,12 +78,12 @@ function f1(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^ a = c; ->a = c : UnknownYesNo -> : ^^^^^^^^^^^^ +>a = c : Choice +> : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ a = d; >a = d : Choice @@ -101,8 +101,8 @@ function f2(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -123,12 +123,12 @@ function f2(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ b = c; ->b = c : UnknownYesNo -> : ^^^^^^^^^^^^ +>b = c : Choice +> : ^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ b = d; >b = d : Choice @@ -146,40 +146,40 @@ function f3(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ c = a; >c = a : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ c = b; >c = b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >b : YesNo > : ^^^^^ c = c; >c = c : Choice.Yes | Choice.No > : ^^^^^^^^^^^^^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >c : Choice.Yes | Choice.No > : ^^^^^^^^^^^^^^^^^^^^^^ c = d; >c = d : Choice > : ^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ } @@ -191,8 +191,8 @@ function f4(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -213,12 +213,12 @@ function f4(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^ d = c; ->d = c : UnknownYesNo -> : ^^^^^^^^^^^^ +>d = c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ d = d; >d = d : Choice @@ -236,8 +236,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -316,8 +316,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c = Choice.Unknown; >c = Choice.Unknown : Choice.Unknown > : ^^^^^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Unknown : Choice.Unknown > : ^^^^^^^^^^^^^^ >Choice : typeof Choice @@ -328,8 +328,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c = Choice.Yes; >c = Choice.Yes : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice @@ -340,8 +340,8 @@ function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c = Choice.No; >c = Choice.No : Choice.No > : ^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.No : Choice.No > : ^^^^^^^^^ >Choice : typeof Choice @@ -393,8 +393,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -473,8 +473,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === Choice.Unknown; >c === Choice.Unknown : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Unknown : Choice.Unknown > : ^^^^^^^^^^^^^^ >Choice : typeof Choice @@ -485,8 +485,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === Choice.Yes; >c === Choice.Yes : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.Yes : Choice.Yes > : ^^^^^^^^^^ >Choice : typeof Choice @@ -497,8 +497,8 @@ function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === Choice.No; >c === Choice.No : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >Choice.No : Choice.No > : ^^^^^^^^^ >Choice : typeof Choice @@ -550,8 +550,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -576,8 +576,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ a === d; >a === d : boolean @@ -608,8 +608,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^ >b : YesNo > : ^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ b === d; >b === d : boolean @@ -622,32 +622,32 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { c === a; >c === a : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >a : Choice.Yes > : ^^^^^^^^^^ c === b; >c === b : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >b : YesNo > : ^^^^^ c === c; >c === c : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ +>c : Choice +> : ^^^^^^ c === d; >c === d : boolean > : ^^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ >d : Choice > : ^^^^^^ @@ -672,8 +672,8 @@ function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { > : ^^^^^^^ >d : Choice > : ^^^^^^ ->c : UnknownYesNo -> : ^^^^^^^^^^^^ +>c : Choice +> : ^^^^^^ d === d; >d === d : boolean @@ -777,12 +777,12 @@ function f11(x: YesNo): YesNo { function f12(x: UnknownYesNo): UnknownYesNo { >f12 : (x: UnknownYesNo) => UnknownYesNo > : ^ ^^ ^^^^^ ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ switch (x) { ->x : UnknownYesNo -> : ^^^^^^^^^^^^ +>x : Choice +> : ^^^^^^ case Choice.Unknown: return x; >Choice.Unknown : Choice.Unknown diff --git a/tests/cases/compiler/identityRelationEnumTypes.ts b/tests/cases/compiler/identityRelationEnumTypes.ts new file mode 100644 index 0000000000000..a647a8828ba33 --- /dev/null +++ b/tests/cases/compiler/identityRelationEnumTypes.ts @@ -0,0 +1,33 @@ +// @strict: true +// @declaration: true + +namespace identityRelationEnumTypes { + type Equals = (() => T extends B ? 1 : 0) extends (() => T extends A ? 1 : 0) ? true : false; + + enum Enum { + A = 'a', + B = 'b', + } + + enum DifferentEnum { + A = 'a', + B = 'b', + } + + type Identical1 = typeof Enum[keyof typeof Enum]; + type Identical2 = Enum.A | Enum.B; + type Identical3 = Enum.B | Enum.A | Enum.B; + type Identical4 = Enum.B | (Enum.A | Enum.B); + type Identical5 = (Enum.A & {}) | (Enum.B & {}) + + type Different1 = Enum.A | Enum.A; + type Different2 = Enum.A | DifferentEnum.B; + + type Result1 = Equals; // true + type Result2 = Equals; // true + type Result3 = Equals; // true + type Result4 = Equals; // true + type Result5 = Equals; // true + type Result6 = Equals; // false + type Result7 = Equals; // false +}