Skip to content

Commit 2077835

Browse files
authored
Merge pull request #18438 from Microsoft/unionIntersectionUnit
Remove empty intersection types in unit types
2 parents d96dfeb + b20d631 commit 2077835

11 files changed

+413
-10
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7298,6 +7298,22 @@ namespace ts {
72987298
return binarySearchTypes(types, type) >= 0;
72997299
}
73007300

7301+
// Return true if the given intersection type contains (a) more than one unit type or (b) an object
7302+
// type and a nullable type (null or undefined).
7303+
function isEmptyIntersectionType(type: IntersectionType) {
7304+
let combined: TypeFlags = 0;
7305+
for (const t of type.types) {
7306+
if (t.flags & TypeFlags.Unit && combined & TypeFlags.Unit) {
7307+
return true;
7308+
}
7309+
combined |= t.flags;
7310+
if (combined & TypeFlags.Nullable && combined & (TypeFlags.Object | TypeFlags.NonPrimitive)) {
7311+
return true;
7312+
}
7313+
}
7314+
return false;
7315+
}
7316+
73017317
function addTypeToUnion(typeSet: TypeSet, type: Type) {
73027318
const flags = type.flags;
73037319
if (flags & TypeFlags.Union) {
@@ -7311,7 +7327,11 @@ namespace ts {
73117327
if (flags & TypeFlags.Null) typeSet.containsNull = true;
73127328
if (!(flags & TypeFlags.ContainsWideningType)) typeSet.containsNonWideningType = true;
73137329
}
7314-
else if (!(flags & TypeFlags.Never)) {
7330+
else if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && isEmptyIntersectionType(<IntersectionType>type))) {
7331+
// We ignore 'never' types in unions. Likewise, we ignore intersections of unit types as they are
7332+
// another form of 'never' (in that they have an empty value domain). We could in theory turn
7333+
// intersections of unit types into 'never' upon construction, but deferring the reduction makes it
7334+
// easier to reason about their origin.
73157335
if (flags & TypeFlags.String) typeSet.containsString = true;
73167336
if (flags & TypeFlags.Number) typeSet.containsNumber = true;
73177337
if (flags & TypeFlags.StringOrNumberLiteral) typeSet.containsStringOrNumberLiteral = true;
@@ -10048,7 +10068,7 @@ namespace ts {
1004810068
}
1004910069

1005010070
function isUnitType(type: Type): boolean {
10051-
return (type.flags & (TypeFlags.Literal | TypeFlags.Undefined | TypeFlags.Null)) !== 0;
10071+
return !!(type.flags & TypeFlags.Unit);
1005210072
}
1005310073

1005410074
function isLiteralType(type: Type): boolean {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,6 +3216,7 @@ namespace ts {
32163216
/* @internal */
32173217
Nullable = Undefined | Null,
32183218
Literal = StringLiteral | NumberLiteral | BooleanLiteral,
3219+
Unit = Literal | Nullable,
32193220
StringOrNumberLiteral = StringLiteral | NumberLiteral,
32203221
/* @internal */
32213222
DefinitelyFalsy = StringLiteral | NumberLiteral | BooleanLiteral | Void | Undefined | Null,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [intersectionOfUnionOfUnitTypes.ts]
2+
// @strict
3+
4+
const enum E { A, B, C, D, E, F }
5+
6+
let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c'
7+
let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c'
8+
let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c'
9+
let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never
10+
let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c'
11+
let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never
12+
13+
let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2
14+
let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2
15+
let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2
16+
let y3: (0 | 1 | 2) & (3 | 4 | 5); // never
17+
let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2
18+
let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never
19+
20+
let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C
21+
let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C
22+
let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C
23+
let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never
24+
let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C
25+
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
26+
27+
28+
//// [intersectionOfUnionOfUnitTypes.js]
29+
// @strict
30+
var x0; // 'a' | 'b' | 'c'
31+
var x1; // 'b' | 'c'
32+
var x2; // 'c'
33+
var x3; // never
34+
var x4; // 'c'
35+
var x5; // never
36+
var y0; // 0 | 1 | 2
37+
var y1; // 1 | 2
38+
var y2; // 2
39+
var y3; // never
40+
var y4; // 2
41+
var y5; // never
42+
var z0; // E.A | E.B | E.C
43+
var z1; // E.B | E.C
44+
var z2; // E.C
45+
var z3; // never
46+
var z4; // E.C
47+
var z5; // never
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
=== tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts ===
2+
// @strict
3+
4+
const enum E { A, B, C, D, E, F }
5+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
6+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
7+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
8+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
9+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
10+
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
11+
>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29))
12+
13+
let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c'
14+
>x0 : Symbol(x0, Decl(intersectionOfUnionOfUnitTypes.ts, 4, 3))
15+
16+
let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c'
17+
>x1 : Symbol(x1, Decl(intersectionOfUnionOfUnitTypes.ts, 5, 3))
18+
19+
let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c'
20+
>x2 : Symbol(x2, Decl(intersectionOfUnionOfUnitTypes.ts, 6, 3))
21+
22+
let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never
23+
>x3 : Symbol(x3, Decl(intersectionOfUnionOfUnitTypes.ts, 7, 3))
24+
25+
let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c'
26+
>x4 : Symbol(x4, Decl(intersectionOfUnionOfUnitTypes.ts, 8, 3))
27+
28+
let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never
29+
>x5 : Symbol(x5, Decl(intersectionOfUnionOfUnitTypes.ts, 9, 3))
30+
31+
let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2
32+
>y0 : Symbol(y0, Decl(intersectionOfUnionOfUnitTypes.ts, 11, 3))
33+
34+
let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2
35+
>y1 : Symbol(y1, Decl(intersectionOfUnionOfUnitTypes.ts, 12, 3))
36+
37+
let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2
38+
>y2 : Symbol(y2, Decl(intersectionOfUnionOfUnitTypes.ts, 13, 3))
39+
40+
let y3: (0 | 1 | 2) & (3 | 4 | 5); // never
41+
>y3 : Symbol(y3, Decl(intersectionOfUnionOfUnitTypes.ts, 14, 3))
42+
43+
let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2
44+
>y4 : Symbol(y4, Decl(intersectionOfUnionOfUnitTypes.ts, 15, 3))
45+
46+
let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never
47+
>y5 : Symbol(y5, Decl(intersectionOfUnionOfUnitTypes.ts, 16, 3))
48+
49+
let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C
50+
>z0 : Symbol(z0, Decl(intersectionOfUnionOfUnitTypes.ts, 18, 3))
51+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
52+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
53+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
54+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
55+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
56+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
57+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
58+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
59+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
60+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
61+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
62+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
63+
64+
let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C
65+
>z1 : Symbol(z1, Decl(intersectionOfUnionOfUnitTypes.ts, 19, 3))
66+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
67+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
68+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
69+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
70+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
71+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
72+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
73+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
74+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
75+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
76+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
77+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
78+
79+
let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C
80+
>z2 : Symbol(z2, Decl(intersectionOfUnionOfUnitTypes.ts, 20, 3))
81+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
82+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
83+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
84+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
85+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
86+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
87+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
88+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
89+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
90+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
91+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
92+
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
93+
94+
let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never
95+
>z3 : Symbol(z3, Decl(intersectionOfUnionOfUnitTypes.ts, 21, 3))
96+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
97+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
98+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
99+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
100+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
101+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
102+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
103+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
104+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
105+
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
106+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
107+
>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29))
108+
109+
let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C
110+
>z4 : Symbol(z4, Decl(intersectionOfUnionOfUnitTypes.ts, 22, 3))
111+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
112+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
113+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
114+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
115+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
116+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
117+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
118+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
119+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
120+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
121+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
122+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
123+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
124+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
125+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
126+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
127+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
128+
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
129+
130+
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
131+
>z5 : Symbol(z5, Decl(intersectionOfUnionOfUnitTypes.ts, 23, 3))
132+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
133+
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
134+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
135+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
136+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
137+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
138+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
139+
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
140+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
141+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
142+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
143+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
144+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
145+
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
146+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
147+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
148+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
149+
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
150+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
151+
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
152+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
153+
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
154+
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
155+
>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29))
156+

0 commit comments

Comments
 (0)