Skip to content

Commit 9e7eedf

Browse files
committed
Smarter algorithm to distribute intersections of unions.
Helps avoid exponential blowup for `keyof` large unions even when `keyof` each type in the union is not a union of unit types (e.g., because there is an index signature or a type variable). Remove the special handling of intersections of unions of unit types because it's no longer needed. This reverts the code changes of pull request #24137 (commit 3fc3df3 with respect to 3fc727b) but keeps the test. Fixes #24223.
1 parent d9ed917 commit 9e7eedf

7 files changed

+415
-41
lines changed

src/compiler/checker.ts

+27-37
Original file line numberDiff line numberDiff line change
@@ -8679,7 +8679,7 @@ namespace ts {
86798679
includes & TypeFlags.Undefined ? includes & TypeFlags.NonWideningType ? undefinedType : undefinedWideningType :
86808680
neverType;
86818681
}
8682-
return getUnionTypeFromSortedList(typeSet, includes & TypeFlags.NotUnit ? 0 : TypeFlags.UnionOfUnitTypes, aliasSymbol, aliasTypeArguments);
8682+
return getUnionTypeFromSortedList(typeSet, aliasSymbol, aliasTypeArguments);
86838683
}
86848684

86858685
function getUnionTypePredicate(signatures: ReadonlyArray<Signature>): TypePredicate | undefined {
@@ -8719,7 +8719,7 @@ namespace ts {
87198719
}
87208720

87218721
// This function assumes the constituent type list is sorted and deduplicated.
8722-
function getUnionTypeFromSortedList(types: Type[], unionOfUnitTypes: TypeFlags, aliasSymbol?: Symbol, aliasTypeArguments?: ReadonlyArray<Type>): Type {
8722+
function getUnionTypeFromSortedList(types: Type[], aliasSymbol?: Symbol, aliasTypeArguments?: ReadonlyArray<Type>): Type {
87238723
if (types.length === 0) {
87248724
return neverType;
87258725
}
@@ -8730,7 +8730,7 @@ namespace ts {
87308730
let type = unionTypes.get(id);
87318731
if (!type) {
87328732
const propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
8733-
type = <UnionType>createType(TypeFlags.Union | propagatedFlags | unionOfUnitTypes);
8733+
type = <UnionType>createType(TypeFlags.Union | propagatedFlags);
87348734
unionTypes.set(id, type);
87358735
type.types = types;
87368736
/*
@@ -8802,29 +8802,6 @@ namespace ts {
88028802
}
88038803
}
88048804

8805-
// When intersecting unions of unit types we can simply intersect based on type identity.
8806-
// Here we remove all unions of unit types from the given list and replace them with a
8807-
// a single union containing an intersection of the unit types.
8808-
function intersectUnionsOfUnitTypes(types: Type[]) {
8809-
const unionIndex = findIndex(types, t => (t.flags & TypeFlags.UnionOfUnitTypes) !== 0);
8810-
const unionType = <UnionType>types[unionIndex];
8811-
let intersection = unionType.types;
8812-
let i = types.length - 1;
8813-
while (i > unionIndex) {
8814-
const t = types[i];
8815-
if (t.flags & TypeFlags.UnionOfUnitTypes) {
8816-
intersection = filter(intersection, u => containsType((<UnionType>t).types, u));
8817-
orderedRemoveItemAt(types, i);
8818-
}
8819-
i--;
8820-
}
8821-
if (intersection === unionType.types) {
8822-
return false;
8823-
}
8824-
types[unionIndex] = getUnionTypeFromSortedList(intersection, unionType.flags & TypeFlags.UnionOfUnitTypes);
8825-
return true;
8826-
}
8827-
88288805
// We normalize combinations of intersection and union types based on the distributive property of the '&'
88298806
// operator. Specifically, because X & (A | B) is equivalent to X & A | X & B, we can transform intersection
88308807
// types with union type constituents into equivalent union types with intersection type constituents and
@@ -8862,18 +8839,31 @@ namespace ts {
88628839
return typeSet[0];
88638840
}
88648841
if (includes & TypeFlags.Union) {
8865-
if (includes & TypeFlags.UnionOfUnitTypes && intersectUnionsOfUnitTypes(typeSet)) {
8866-
// When the intersection creates a reduced set (which might mean that *all* union types have
8867-
// disappeared), we restart the operation to get a new set of combined flags. Once we have
8868-
// reduced we'll never reduce again, so this occurs at most once.
8869-
return getIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
8870-
}
88718842
// We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
88728843
// the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
8873-
const unionIndex = findIndex(typeSet, t => (t.flags & TypeFlags.Union) !== 0);
8874-
const unionType = <UnionType>typeSet[unionIndex];
8875-
return getUnionType(map(unionType.types, t => getIntersectionType(replaceElement(typeSet, unionIndex, t))),
8876-
UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
8844+
const lastNonfinalUnion = findLastIndex(typeSet, t => (t.flags & TypeFlags.Union) !== 0, typeSet.length - 2);
8845+
if (lastNonfinalUnion === -1) {
8846+
// typeSet[typeSet.length - 1] must be the only union. Distribute it and we're done.
8847+
return getUnionType(map((<UnionType>typeSet[typeSet.length - 1]).types, t => getIntersectionType(replaceElement(typeSet, typeSet.length - 1, t))),
8848+
UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
8849+
}
8850+
else {
8851+
// `keyof` a large union of types results in an intersection of unions containing many unit types (GH#24223).
8852+
// To help avoid an exponential blowup, distribute the last union over the remaining constituents of the
8853+
// intersection and simplify the resulting union before distributing earlier unions. (Exception: don't
8854+
// distribute a union that is the last constituent of the intersection over the zero remaining constituents
8855+
// because that would have no effect.)
8856+
const unionType = <UnionType>typeSet[lastNonfinalUnion];
8857+
const laterIntersectedTypes = typeSet.slice(lastNonfinalUnion + 1);
8858+
if (lastNonfinalUnion === 0) {
8859+
return getUnionType(map(unionType.types, t => getIntersectionType([t].concat(laterIntersectedTypes))),
8860+
UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
8861+
}
8862+
else {
8863+
const partialIntersection = getUnionType(map(unionType.types, t => getIntersectionType([t].concat(laterIntersectedTypes))), UnionReduction.Literal);
8864+
return getIntersectionType(typeSet.slice(0, lastNonfinalUnion).concat(partialIntersection), aliasSymbol, aliasTypeArguments);
8865+
}
8866+
}
88778867
}
88788868
const id = getTypeListId(typeSet);
88798869
let type = intersectionTypes.get(id);
@@ -13953,7 +13943,7 @@ namespace ts {
1395313943
if (type.flags & TypeFlags.Union) {
1395413944
const types = (<UnionType>type).types;
1395513945
const filtered = filter(types, f);
13956-
return filtered === types ? type : getUnionTypeFromSortedList(filtered, type.flags & TypeFlags.UnionOfUnitTypes);
13946+
return filtered === types ? type : getUnionTypeFromSortedList(filtered);
1395713947
}
1395813948
return f(type) ? type : neverType;
1395913949
}

src/compiler/types.ts

-4
Original file line numberDiff line numberDiff line change
@@ -3673,8 +3673,6 @@ namespace ts {
36733673
/* @internal */
36743674
FreshLiteral = 1 << 25, // Fresh literal or unique type
36753675
/* @internal */
3676-
UnionOfUnitTypes = 1 << 26, // Type is union of unit types
3677-
/* @internal */
36783676
ContainsWideningType = 1 << 27, // Type is or contains undefined or null widening type
36793677
/* @internal */
36803678
ContainsObjectLiteral = 1 << 28, // Type is or contains object literal type
@@ -3718,8 +3716,6 @@ namespace ts {
37183716
Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
37193717
NotUnionOrUnit = Any | Unknown | ESSymbol | Object | NonPrimitive,
37203718
/* @internal */
3721-
NotUnit = Any | String | Number | Boolean | Enum | ESSymbol | Void | Never | StructuredOrInstantiable,
3722-
/* @internal */
37233719
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
37243720
/* @internal */
37253721
PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
tests/cases/compiler/intersectionsOfLargeUnions2.ts(31,15): error TS2536: Type 'T' cannot be used to index type 'HTMLElementTagNameMap'.
2+
tests/cases/compiler/intersectionsOfLargeUnions2.ts(31,15): error TS2536: Type 'P' cannot be used to index type 'HTMLElementTagNameMap[T]'.
3+
4+
5+
==== tests/cases/compiler/intersectionsOfLargeUnions2.ts (2 errors) ====
6+
// Repro from #24223
7+
8+
declare global {
9+
interface ElementTagNameMap {
10+
[index: number]: HTMLElement
11+
}
12+
13+
interface HTMLElement {
14+
[index: number]: HTMLElement;
15+
}
16+
}
17+
18+
export function assertIsElement(node: Node | null): node is Element {
19+
let nodeType = node === null ? null : node.nodeType;
20+
return nodeType === 1;
21+
}
22+
23+
export function assertNodeTagName<
24+
T extends keyof ElementTagNameMap,
25+
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
26+
if (assertIsElement(node)) {
27+
const nodeTagName = node.tagName.toLowerCase();
28+
return nodeTagName === tagName;
29+
}
30+
return false;
31+
}
32+
33+
export function assertNodeProperty<
34+
T extends keyof ElementTagNameMap,
35+
P extends keyof ElementTagNameMap[T],
36+
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
37+
~~~~~~~~~~~~~~~~~~~~~~~~
38+
!!! error TS2536: Type 'T' cannot be used to index type 'HTMLElementTagNameMap'.
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
!!! error TS2536: Type 'P' cannot be used to index type 'HTMLElementTagNameMap[T]'.
41+
if (assertNodeTagName(node, tagName)) {
42+
node[prop];
43+
}
44+
}
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//// [intersectionsOfLargeUnions2.ts]
2+
// Repro from #24223
3+
4+
declare global {
5+
interface ElementTagNameMap {
6+
[index: number]: HTMLElement
7+
}
8+
9+
interface HTMLElement {
10+
[index: number]: HTMLElement;
11+
}
12+
}
13+
14+
export function assertIsElement(node: Node | null): node is Element {
15+
let nodeType = node === null ? null : node.nodeType;
16+
return nodeType === 1;
17+
}
18+
19+
export function assertNodeTagName<
20+
T extends keyof ElementTagNameMap,
21+
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
22+
if (assertIsElement(node)) {
23+
const nodeTagName = node.tagName.toLowerCase();
24+
return nodeTagName === tagName;
25+
}
26+
return false;
27+
}
28+
29+
export function assertNodeProperty<
30+
T extends keyof ElementTagNameMap,
31+
P extends keyof ElementTagNameMap[T],
32+
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
33+
if (assertNodeTagName(node, tagName)) {
34+
node[prop];
35+
}
36+
}
37+
38+
39+
//// [intersectionsOfLargeUnions2.js]
40+
"use strict";
41+
// Repro from #24223
42+
exports.__esModule = true;
43+
function assertIsElement(node) {
44+
var nodeType = node === null ? null : node.nodeType;
45+
return nodeType === 1;
46+
}
47+
exports.assertIsElement = assertIsElement;
48+
function assertNodeTagName(node, tagName) {
49+
if (assertIsElement(node)) {
50+
var nodeTagName = node.tagName.toLowerCase();
51+
return nodeTagName === tagName;
52+
}
53+
return false;
54+
}
55+
exports.assertNodeTagName = assertNodeTagName;
56+
function assertNodeProperty(node, tagName, prop, value) {
57+
if (assertNodeTagName(node, tagName)) {
58+
node[prop];
59+
}
60+
}
61+
exports.assertNodeProperty = assertNodeProperty;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
=== tests/cases/compiler/intersectionsOfLargeUnions2.ts ===
2+
// Repro from #24223
3+
4+
declare global {
5+
>global : Symbol(global, Decl(intersectionsOfLargeUnions2.ts, 0, 0))
6+
7+
interface ElementTagNameMap {
8+
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.dom.d.ts, --, --), Decl(intersectionsOfLargeUnions2.ts, 2, 16))
9+
10+
[index: number]: HTMLElement
11+
>index : Symbol(index, Decl(intersectionsOfLargeUnions2.ts, 4, 9))
12+
>HTMLElement : Symbol(HTMLElement, Decl(intersectionsOfLargeUnions2.ts, 5, 5))
13+
}
14+
15+
interface HTMLElement {
16+
>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --), Decl(intersectionsOfLargeUnions2.ts, 5, 5))
17+
18+
[index: number]: HTMLElement;
19+
>index : Symbol(index, Decl(intersectionsOfLargeUnions2.ts, 8, 9))
20+
>HTMLElement : Symbol(HTMLElement, Decl(intersectionsOfLargeUnions2.ts, 5, 5))
21+
}
22+
}
23+
24+
export function assertIsElement(node: Node | null): node is Element {
25+
>assertIsElement : Symbol(assertIsElement, Decl(intersectionsOfLargeUnions2.ts, 10, 1))
26+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 12, 32))
27+
>Node : Symbol(Node, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
28+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 12, 32))
29+
>Element : Symbol(Element, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
30+
31+
let nodeType = node === null ? null : node.nodeType;
32+
>nodeType : Symbol(nodeType, Decl(intersectionsOfLargeUnions2.ts, 13, 7))
33+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 12, 32))
34+
>node.nodeType : Symbol(Node.nodeType, Decl(lib.dom.d.ts, --, --))
35+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 12, 32))
36+
>nodeType : Symbol(Node.nodeType, Decl(lib.dom.d.ts, --, --))
37+
38+
return nodeType === 1;
39+
>nodeType : Symbol(nodeType, Decl(intersectionsOfLargeUnions2.ts, 13, 7))
40+
}
41+
42+
export function assertNodeTagName<
43+
>assertNodeTagName : Symbol(assertNodeTagName, Decl(intersectionsOfLargeUnions2.ts, 15, 1))
44+
45+
T extends keyof ElementTagNameMap,
46+
>T : Symbol(T, Decl(intersectionsOfLargeUnions2.ts, 17, 34))
47+
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.dom.d.ts, --, --), Decl(intersectionsOfLargeUnions2.ts, 2, 16))
48+
49+
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
50+
>U : Symbol(U, Decl(intersectionsOfLargeUnions2.ts, 18, 38))
51+
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.dom.d.ts, --, --), Decl(intersectionsOfLargeUnions2.ts, 2, 16))
52+
>T : Symbol(T, Decl(intersectionsOfLargeUnions2.ts, 17, 34))
53+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 19, 36))
54+
>Node : Symbol(Node, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
55+
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions2.ts, 19, 54))
56+
>T : Symbol(T, Decl(intersectionsOfLargeUnions2.ts, 17, 34))
57+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 19, 36))
58+
>U : Symbol(U, Decl(intersectionsOfLargeUnions2.ts, 18, 38))
59+
60+
if (assertIsElement(node)) {
61+
>assertIsElement : Symbol(assertIsElement, Decl(intersectionsOfLargeUnions2.ts, 10, 1))
62+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 19, 36))
63+
64+
const nodeTagName = node.tagName.toLowerCase();
65+
>nodeTagName : Symbol(nodeTagName, Decl(intersectionsOfLargeUnions2.ts, 21, 13))
66+
>node.tagName.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
67+
>node.tagName : Symbol(Element.tagName, Decl(lib.dom.d.ts, --, --))
68+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 19, 36))
69+
>tagName : Symbol(Element.tagName, Decl(lib.dom.d.ts, --, --))
70+
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
71+
72+
return nodeTagName === tagName;
73+
>nodeTagName : Symbol(nodeTagName, Decl(intersectionsOfLargeUnions2.ts, 21, 13))
74+
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions2.ts, 19, 54))
75+
}
76+
return false;
77+
}
78+
79+
export function assertNodeProperty<
80+
>assertNodeProperty : Symbol(assertNodeProperty, Decl(intersectionsOfLargeUnions2.ts, 25, 1))
81+
82+
T extends keyof ElementTagNameMap,
83+
>T : Symbol(T, Decl(intersectionsOfLargeUnions2.ts, 27, 35))
84+
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.dom.d.ts, --, --), Decl(intersectionsOfLargeUnions2.ts, 2, 16))
85+
86+
P extends keyof ElementTagNameMap[T],
87+
>P : Symbol(P, Decl(intersectionsOfLargeUnions2.ts, 28, 38))
88+
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.dom.d.ts, --, --), Decl(intersectionsOfLargeUnions2.ts, 2, 16))
89+
>T : Symbol(T, Decl(intersectionsOfLargeUnions2.ts, 27, 35))
90+
91+
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
92+
>V : Symbol(V, Decl(intersectionsOfLargeUnions2.ts, 29, 41))
93+
>HTMLElementTagNameMap : Symbol(HTMLElementTagNameMap, Decl(lib.dom.d.ts, --, --))
94+
>T : Symbol(T, Decl(intersectionsOfLargeUnions2.ts, 27, 35))
95+
>P : Symbol(P, Decl(intersectionsOfLargeUnions2.ts, 28, 38))
96+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 30, 43))
97+
>Node : Symbol(Node, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
98+
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions2.ts, 30, 61))
99+
>T : Symbol(T, Decl(intersectionsOfLargeUnions2.ts, 27, 35))
100+
>prop : Symbol(prop, Decl(intersectionsOfLargeUnions2.ts, 30, 73))
101+
>P : Symbol(P, Decl(intersectionsOfLargeUnions2.ts, 28, 38))
102+
>value : Symbol(value, Decl(intersectionsOfLargeUnions2.ts, 30, 82))
103+
>V : Symbol(V, Decl(intersectionsOfLargeUnions2.ts, 29, 41))
104+
105+
if (assertNodeTagName(node, tagName)) {
106+
>assertNodeTagName : Symbol(assertNodeTagName, Decl(intersectionsOfLargeUnions2.ts, 15, 1))
107+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 30, 43))
108+
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions2.ts, 30, 61))
109+
110+
node[prop];
111+
>node : Symbol(node, Decl(intersectionsOfLargeUnions2.ts, 30, 43))
112+
>prop : Symbol(prop, Decl(intersectionsOfLargeUnions2.ts, 30, 73))
113+
}
114+
}
115+

0 commit comments

Comments
 (0)