From 4885560cb4a12c41f172c1bfdeedea806400b3e3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 7 Sep 2017 16:02:00 -0700 Subject: [PATCH 1/6] Eliminate intersections of unit types in union types --- src/compiler/checker.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 91653c140c83e..379bc3377d230 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7302,7 +7302,11 @@ namespace ts { if (flags & TypeFlags.Null) typeSet.containsNull = true; if (!(flags & TypeFlags.ContainsWideningType)) typeSet.containsNonWideningType = true; } - else if (!(flags & TypeFlags.Never)) { + else if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && every((type).types, isUnitType))) { + // We ignore 'never' types in unions. Likewise, we ignore intersections of unit types as they are + // another form of 'never' (in that they have an empty value domain). We could in theory turn + // intersections of unit types into 'never' upon construction, but deferring the reduction makes it + // easier to reason about their origin. if (flags & TypeFlags.String) typeSet.containsString = true; if (flags & TypeFlags.Number) typeSet.containsNumber = true; if (flags & TypeFlags.StringOrNumberLiteral) typeSet.containsStringOrNumberLiteral = true; From 6c2fe29a72e8ad8d9ca178eeafe8d9bfa7a5b6bc Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 11 Sep 2017 11:02:11 -0700 Subject: [PATCH 2/6] Accept new baselines --- tests/baselines/reference/keyofAndIndexedAccess.types | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baselines/reference/keyofAndIndexedAccess.types b/tests/baselines/reference/keyofAndIndexedAccess.types index 25d79e3f4ff42..4bd5bb4e3c488 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.types +++ b/tests/baselines/reference/keyofAndIndexedAccess.types @@ -205,7 +205,7 @@ type Q40 = (Shape | Options)["visible"]; // boolean | "yes" | "no" >Options : Options type Q41 = (Shape & Options)["visible"]; // true & "yes" | true & "no" | false & "yes" | false & "no" ->Q41 : (true & "yes") | (true & "no") | (false & "yes") | (false & "no") +>Q41 : never >Shape : Shape >Options : Options From 78f4cbe53c8696d6f8b23424fcfd666c7d71ba01 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 13 Sep 2017 06:37:59 -0700 Subject: [PATCH 3/6] Add tests --- .../intersectionOfUnionOfUnitTypes.js | 47 ++++++ .../intersectionOfUnionOfUnitTypes.symbols | 156 ++++++++++++++++++ .../intersectionOfUnionOfUnitTypes.types | 156 ++++++++++++++++++ .../intersectionOfUnionOfUnitTypes.ts | 24 +++ 4 files changed, 383 insertions(+) create mode 100644 tests/baselines/reference/intersectionOfUnionOfUnitTypes.js create mode 100644 tests/baselines/reference/intersectionOfUnionOfUnitTypes.symbols create mode 100644 tests/baselines/reference/intersectionOfUnionOfUnitTypes.types create mode 100644 tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts diff --git a/tests/baselines/reference/intersectionOfUnionOfUnitTypes.js b/tests/baselines/reference/intersectionOfUnionOfUnitTypes.js new file mode 100644 index 0000000000000..09a2ab974f4de --- /dev/null +++ b/tests/baselines/reference/intersectionOfUnionOfUnitTypes.js @@ -0,0 +1,47 @@ +//// [intersectionOfUnionOfUnitTypes.ts] +// @strict + +const enum E { A, B, C, D, E, F } + +let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c' +let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c' +let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c' +let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never +let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c' +let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never + +let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2 +let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2 +let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2 +let y3: (0 | 1 | 2) & (3 | 4 | 5); // never +let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2 +let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never + +let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C +let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C +let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C +let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never +let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C +let z5: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E) & (E.D | E.E | E.F); // never + + +//// [intersectionOfUnionOfUnitTypes.js] +// @strict +var x0; // 'a' | 'b' | 'c' +var x1; // 'b' | 'c' +var x2; // 'c' +var x3; // never +var x4; // 'c' +var x5; // never +var y0; // 0 | 1 | 2 +var y1; // 1 | 2 +var y2; // 2 +var y3; // never +var y4; // 2 +var y5; // never +var z0; // E.A | E.B | E.C +var z1; // E.B | E.C +var z2; // E.C +var z3; // never +var z4; // E.C +var z5; // never diff --git a/tests/baselines/reference/intersectionOfUnionOfUnitTypes.symbols b/tests/baselines/reference/intersectionOfUnionOfUnitTypes.symbols new file mode 100644 index 0000000000000..6f5f70bd29008 --- /dev/null +++ b/tests/baselines/reference/intersectionOfUnionOfUnitTypes.symbols @@ -0,0 +1,156 @@ +=== tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts === +// @strict + +const enum E { A, B, C, D, E, F } +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26)) +>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29)) + +let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c' +>x0 : Symbol(x0, Decl(intersectionOfUnionOfUnitTypes.ts, 4, 3)) + +let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c' +>x1 : Symbol(x1, Decl(intersectionOfUnionOfUnitTypes.ts, 5, 3)) + +let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c' +>x2 : Symbol(x2, Decl(intersectionOfUnionOfUnitTypes.ts, 6, 3)) + +let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never +>x3 : Symbol(x3, Decl(intersectionOfUnionOfUnitTypes.ts, 7, 3)) + +let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c' +>x4 : Symbol(x4, Decl(intersectionOfUnionOfUnitTypes.ts, 8, 3)) + +let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never +>x5 : Symbol(x5, Decl(intersectionOfUnionOfUnitTypes.ts, 9, 3)) + +let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2 +>y0 : Symbol(y0, Decl(intersectionOfUnionOfUnitTypes.ts, 11, 3)) + +let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2 +>y1 : Symbol(y1, Decl(intersectionOfUnionOfUnitTypes.ts, 12, 3)) + +let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2 +>y2 : Symbol(y2, Decl(intersectionOfUnionOfUnitTypes.ts, 13, 3)) + +let y3: (0 | 1 | 2) & (3 | 4 | 5); // never +>y3 : Symbol(y3, Decl(intersectionOfUnionOfUnitTypes.ts, 14, 3)) + +let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2 +>y4 : Symbol(y4, Decl(intersectionOfUnionOfUnitTypes.ts, 15, 3)) + +let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never +>y5 : Symbol(y5, Decl(intersectionOfUnionOfUnitTypes.ts, 16, 3)) + +let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C +>z0 : Symbol(z0, Decl(intersectionOfUnionOfUnitTypes.ts, 18, 3)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) + +let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C +>z1 : Symbol(z1, Decl(intersectionOfUnionOfUnitTypes.ts, 19, 3)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) + +let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C +>z2 : Symbol(z2, Decl(intersectionOfUnionOfUnitTypes.ts, 20, 3)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26)) + +let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never +>z3 : Symbol(z3, Decl(intersectionOfUnionOfUnitTypes.ts, 21, 3)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29)) + +let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C +>z4 : Symbol(z4, Decl(intersectionOfUnionOfUnitTypes.ts, 22, 3)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26)) + +let z5: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E) & (E.D | E.E | E.F); // never +>z5 : Symbol(z5, Decl(intersectionOfUnionOfUnitTypes.ts, 23, 3)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26)) +>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0)) +>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29)) + diff --git a/tests/baselines/reference/intersectionOfUnionOfUnitTypes.types b/tests/baselines/reference/intersectionOfUnionOfUnitTypes.types new file mode 100644 index 0000000000000..aa7fa0074edaf --- /dev/null +++ b/tests/baselines/reference/intersectionOfUnionOfUnitTypes.types @@ -0,0 +1,156 @@ +=== tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts === +// @strict + +const enum E { A, B, C, D, E, F } +>E : E +>A : E.A +>B : E.B +>C : E.C +>D : E.D +>E : E.E +>F : E.F + +let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c' +>x0 : "a" | "b" | "c" + +let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c' +>x1 : "b" | "c" + +let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c' +>x2 : "c" + +let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never +>x3 : never + +let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c' +>x4 : "c" + +let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never +>x5 : never + +let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2 +>y0 : 0 | 1 | 2 + +let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2 +>y1 : 1 | 2 + +let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2 +>y2 : 2 + +let y3: (0 | 1 | 2) & (3 | 4 | 5); // never +>y3 : never + +let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2 +>y4 : 2 + +let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never +>y5 : never + +let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C +>z0 : E.A | E.B | E.C +>E : any +>A : E.A +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>A : E.A +>E : any +>B : E.B +>E : any +>C : E.C + +let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C +>z1 : E.B | E.C +>E : any +>A : E.A +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>D : E.D + +let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C +>z2 : E.C +>E : any +>A : E.A +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>C : E.C +>E : any +>D : E.D +>E : any +>E : E.E + +let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never +>z3 : never +>E : any +>A : E.A +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>D : E.D +>E : any +>E : E.E +>E : any +>F : E.F + +let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C +>z4 : E.C +>E : any +>A : E.A +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>D : E.D +>E : any +>C : E.C +>E : any +>D : E.D +>E : any +>E : E.E + +let z5: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E) & (E.D | E.E | E.F); // never +>z5 : never +>E : any +>A : E.A +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>B : E.B +>E : any +>C : E.C +>E : any +>D : E.D +>E : any +>C : E.C +>E : any +>D : E.D +>E : any +>E : E.E +>E : any +>D : E.D +>E : any +>E : E.E +>E : any +>F : E.F + diff --git a/tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts b/tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts new file mode 100644 index 0000000000000..28492b1d94bd5 --- /dev/null +++ b/tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts @@ -0,0 +1,24 @@ +// @strict + +const enum E { A, B, C, D, E, F } + +let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c' +let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c' +let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c' +let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never +let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c' +let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never + +let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2 +let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2 +let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2 +let y3: (0 | 1 | 2) & (3 | 4 | 5); // never +let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2 +let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never + +let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C +let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C +let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C +let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never +let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C +let z5: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E) & (E.D | E.E | E.F); // never From c64beb90dfb513cbc24ed4a4a52b242196f0c2a3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 13 Sep 2017 11:52:10 -0700 Subject: [PATCH 4/6] Remove intersections of object and nullable types from union types --- src/compiler/checker.ts | 20 ++++++++++++++++++-- src/compiler/types.ts | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 379bc3377d230..bdebfcef47a35 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7289,6 +7289,22 @@ namespace ts { return binarySearchTypes(types, type) >= 0; } + // Return true if the given intersection type contains (a) more than one unit type or (b) an object + // type and a nullable type (null or undefined). + function isEmptyIntersectionType(type: IntersectionType) { + let combined: TypeFlags = 0; + for (const t of type.types) { + if (t.flags & TypeFlags.Unit && combined & TypeFlags.Unit) { + return true; + } + combined |= t.flags; + if (combined & TypeFlags.Nullable && combined & (TypeFlags.Object | TypeFlags.NonPrimitive)) { + return true; + } + } + return false; + } + function addTypeToUnion(typeSet: TypeSet, type: Type) { const flags = type.flags; if (flags & TypeFlags.Union) { @@ -7302,7 +7318,7 @@ namespace ts { if (flags & TypeFlags.Null) typeSet.containsNull = true; if (!(flags & TypeFlags.ContainsWideningType)) typeSet.containsNonWideningType = true; } - else if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && every((type).types, isUnitType))) { + else if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && isEmptyIntersectionType(type))) { // We ignore 'never' types in unions. Likewise, we ignore intersections of unit types as they are // another form of 'never' (in that they have an empty value domain). We could in theory turn // intersections of unit types into 'never' upon construction, but deferring the reduction makes it @@ -10041,7 +10057,7 @@ namespace ts { } function isUnitType(type: Type): boolean { - return (type.flags & (TypeFlags.Literal | TypeFlags.Undefined | TypeFlags.Null)) !== 0; + return !!(type.flags & TypeFlags.Unit); } function isLiteralType(type: Type): boolean { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 55baf9763c2ed..efae0b50784f7 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3217,6 +3217,7 @@ namespace ts { /* @internal */ Nullable = Undefined | Null, Literal = StringLiteral | NumberLiteral | BooleanLiteral, + Unit = Literal | Nullable, StringOrNumberLiteral = StringLiteral | NumberLiteral, /* @internal */ DefinitelyFalsy = StringLiteral | NumberLiteral | BooleanLiteral | Void | Undefined | Null, From 0ac942f7ab89ccbe42af987425237f3a271b32a3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 13 Sep 2017 11:52:21 -0700 Subject: [PATCH 5/6] Update test --- tests/cases/compiler/restUnion2.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/compiler/restUnion2.ts b/tests/cases/compiler/restUnion2.ts index 83d94e03a7365..21f912cb2fb01 100644 --- a/tests/cases/compiler/restUnion2.ts +++ b/tests/cases/compiler/restUnion2.ts @@ -14,6 +14,6 @@ declare const nullAndUndefinedUnion: null | undefined; var rest4: { }; var {...rest4 } = nullAndUndefinedUnion; -declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined | null; +declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined; var rest5: { n: number, s: string }; var {...rest5 } = unionWithIntersection; \ No newline at end of file From b20d631ba235ae176d12fe95006535b637357e01 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 13 Sep 2017 11:52:51 -0700 Subject: [PATCH 6/6] Accept new baselines --- tests/baselines/reference/restUnion2.js | 2 +- tests/baselines/reference/restUnion2.symbols | 2 +- tests/baselines/reference/restUnion2.types | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/baselines/reference/restUnion2.js b/tests/baselines/reference/restUnion2.js index 71f4b06cfef48..382b7bb2bc916 100644 --- a/tests/baselines/reference/restUnion2.js +++ b/tests/baselines/reference/restUnion2.js @@ -13,7 +13,7 @@ declare const nullAndUndefinedUnion: null | undefined; var rest4: { }; var {...rest4 } = nullAndUndefinedUnion; -declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined | null; +declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined; var rest5: { n: number, s: string }; var {...rest5 } = unionWithIntersection; diff --git a/tests/baselines/reference/restUnion2.symbols b/tests/baselines/reference/restUnion2.symbols index 54ac47f06940b..2728285f5ef17 100644 --- a/tests/baselines/reference/restUnion2.symbols +++ b/tests/baselines/reference/restUnion2.symbols @@ -35,7 +35,7 @@ var {...rest4 } = nullAndUndefinedUnion; >rest4 : Symbol(rest4, Decl(restUnion2.ts, 11, 3), Decl(restUnion2.ts, 12, 5)) >nullAndUndefinedUnion : Symbol(nullAndUndefinedUnion, Decl(restUnion2.ts, 10, 13)) -declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined | null; +declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined; >unionWithIntersection : Symbol(unionWithIntersection, Decl(restUnion2.ts, 14, 13)) >n : Symbol(n, Decl(restUnion2.ts, 14, 39)) >s : Symbol(s, Decl(restUnion2.ts, 14, 55)) diff --git a/tests/baselines/reference/restUnion2.types b/tests/baselines/reference/restUnion2.types index 03c8d577e668d..029192a9ac46c 100644 --- a/tests/baselines/reference/restUnion2.types +++ b/tests/baselines/reference/restUnion2.types @@ -37,11 +37,10 @@ var {...rest4 } = nullAndUndefinedUnion; >rest4 : {} >nullAndUndefinedUnion : null | undefined -declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined | null; ->unionWithIntersection : ({ n: number; } & { s: string; } & undefined) | null +declare const unionWithIntersection: ({ n: number } & { s: string }) & undefined; +>unionWithIntersection : { n: number; } & { s: string; } & undefined >n : number >s : string ->null : null var rest5: { n: number, s: string }; >rest5 : { n: number; s: string; } @@ -50,5 +49,5 @@ var rest5: { n: number, s: string }; var {...rest5 } = unionWithIntersection; >rest5 : { n: number; s: string; } ->unionWithIntersection : ({ n: number; } & { s: string; } & undefined) | null +>unionWithIntersection : { n: number; } & { s: string; } & undefined