Skip to content

Commit 7b4d13c

Browse files
authored
Merge pull request #25859 from Microsoft/optimizePrimitiveIntersections
Optimize creation of intersections of union types
2 parents dd4fd8c + f9681e2 commit 7b4d13c

7 files changed

+453
-19
lines changed

src/compiler/checker.ts

+63-17
Original file line numberDiff line numberDiff line change
@@ -8529,6 +8529,15 @@ namespace ts {
85298529
return binarySearch(types, type, getTypeId, compareValues) >= 0;
85308530
}
85318531

8532+
function insertType(types: Type[], type: Type): boolean {
8533+
const index = binarySearch(types, type, getTypeId, compareValues);
8534+
if (index < 0) {
8535+
types.splice(~index, 0, type);
8536+
return true;
8537+
}
8538+
return false;
8539+
}
8540+
85328541
// Return true if the given intersection type contains
85338542
// more than one unit type or,
85348543
// an object type and a nullable type (null or undefined), or
@@ -8696,7 +8705,7 @@ namespace ts {
86968705
includes & TypeFlags.Undefined ? includes & TypeFlags.NonWideningType ? undefinedType : undefinedWideningType :
86978706
neverType;
86988707
}
8699-
return getUnionTypeFromSortedList(typeSet, includes & TypeFlags.NotUnit ? 0 : TypeFlags.UnionOfUnitTypes, aliasSymbol, aliasTypeArguments);
8708+
return getUnionTypeFromSortedList(typeSet, includes & TypeFlags.NotPrimitiveUnion ? 0 : TypeFlags.UnionOfPrimitiveTypes, aliasSymbol, aliasTypeArguments);
87008709
}
87018710

87028711
function getUnionTypePredicate(signatures: ReadonlyArray<Signature>): TypePredicate | undefined {
@@ -8819,26 +8828,63 @@ namespace ts {
88198828
}
88208829
}
88218830

8822-
// When intersecting unions of unit types we can simply intersect based on type identity.
8823-
// Here we remove all unions of unit types from the given list and replace them with a
8824-
// a single union containing an intersection of the unit types.
8825-
function intersectUnionsOfUnitTypes(types: Type[]) {
8826-
const unionIndex = findIndex(types, t => (t.flags & TypeFlags.UnionOfUnitTypes) !== 0);
8827-
const unionType = <UnionType>types[unionIndex];
8828-
let intersection = unionType.types;
8829-
let i = types.length - 1;
8830-
while (i > unionIndex) {
8831+
// Check that the given type has a match in every union. A given type is matched by
8832+
// an identical type, and a literal type is additionally matched by its corresponding
8833+
// primitive type.
8834+
function eachUnionContains(unionTypes: UnionType[], type: Type) {
8835+
for (const u of unionTypes) {
8836+
if (!containsType(u.types, type)) {
8837+
const primitive = type.flags & TypeFlags.StringLiteral ? stringType :
8838+
type.flags & TypeFlags.NumberLiteral ? numberType :
8839+
type.flags & TypeFlags.UniqueESSymbol ? esSymbolType :
8840+
undefined;
8841+
if (!primitive || !containsType(u.types, primitive)) {
8842+
return false;
8843+
}
8844+
}
8845+
}
8846+
return true;
8847+
}
8848+
8849+
// If the given list of types contains more than one union of primitive types, replace the
8850+
// first with a union containing an intersection of those primitive types, then remove the
8851+
// other unions and return true. Otherwise, do nothing and return false.
8852+
function intersectUnionsOfPrimitiveTypes(types: Type[]) {
8853+
let unionTypes: UnionType[] | undefined;
8854+
const index = findIndex(types, t => (t.flags & TypeFlags.UnionOfPrimitiveTypes) !== 0);
8855+
let i = index + 1;
8856+
// Remove all but the first union of primitive types and collect them in
8857+
// the unionTypes array.
8858+
while (i < types.length) {
88318859
const t = types[i];
8832-
if (t.flags & TypeFlags.UnionOfUnitTypes) {
8833-
intersection = filter(intersection, u => containsType((<UnionType>t).types, u));
8860+
if (t.flags & TypeFlags.UnionOfPrimitiveTypes) {
8861+
(unionTypes || (unionTypes = [<UnionType>types[index]])).push(<UnionType>t);
88348862
orderedRemoveItemAt(types, i);
88358863
}
8836-
i--;
8864+
else {
8865+
i++;
8866+
}
88378867
}
8838-
if (intersection === unionType.types) {
8868+
// Return false if there was only one union of primitive types
8869+
if (!unionTypes) {
88398870
return false;
88408871
}
8841-
types[unionIndex] = getUnionTypeFromSortedList(intersection, unionType.flags & TypeFlags.UnionOfUnitTypes);
8872+
// We have more than one union of primitive types, now intersect them. For each
8873+
// type in each union we check if the type is matched in every union and if so
8874+
// we include it in the result.
8875+
const checked: Type[] = [];
8876+
const result: Type[] = [];
8877+
for (const u of unionTypes) {
8878+
for (const t of u.types) {
8879+
if (insertType(checked, t)) {
8880+
if (eachUnionContains(unionTypes, t)) {
8881+
insertType(result, t);
8882+
}
8883+
}
8884+
}
8885+
}
8886+
// Finally replace the first union with the result
8887+
types[index] = getUnionTypeFromSortedList(result, TypeFlags.UnionOfPrimitiveTypes);
88428888
return true;
88438889
}
88448890

@@ -8879,7 +8925,7 @@ namespace ts {
88798925
return typeSet[0];
88808926
}
88818927
if (includes & TypeFlags.Union) {
8882-
if (includes & TypeFlags.UnionOfUnitTypes && intersectUnionsOfUnitTypes(typeSet)) {
8928+
if (includes & TypeFlags.UnionOfPrimitiveTypes && intersectUnionsOfPrimitiveTypes(typeSet)) {
88838929
// When the intersection creates a reduced set (which might mean that *all* union types have
88848930
// disappeared), we restart the operation to get a new set of combined flags. Once we have
88858931
// reduced we'll never reduce again, so this occurs at most once.
@@ -13976,7 +14022,7 @@ namespace ts {
1397614022
if (type.flags & TypeFlags.Union) {
1397714023
const types = (<UnionType>type).types;
1397814024
const filtered = filter(types, f);
13979-
return filtered === types ? type : getUnionTypeFromSortedList(filtered, type.flags & TypeFlags.UnionOfUnitTypes);
14025+
return filtered === types ? type : getUnionTypeFromSortedList(filtered, type.flags & TypeFlags.UnionOfPrimitiveTypes);
1398014026
}
1398114027
return f(type) ? type : neverType;
1398214028
}

src/compiler/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3675,7 +3675,7 @@ namespace ts {
36753675
/* @internal */
36763676
FreshLiteral = 1 << 25, // Fresh literal or unique type
36773677
/* @internal */
3678-
UnionOfUnitTypes = 1 << 26, // Type is union of unit types
3678+
UnionOfPrimitiveTypes = 1 << 26, // Type is union of primitive types
36793679
/* @internal */
36803680
ContainsWideningType = 1 << 27, // Type is or contains undefined or null widening type
36813681
/* @internal */
@@ -3720,7 +3720,7 @@ namespace ts {
37203720
Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
37213721
NotUnionOrUnit = Any | Unknown | ESSymbol | Object | NonPrimitive,
37223722
/* @internal */
3723-
NotUnit = Any | String | Number | Boolean | Enum | ESSymbol | Void | Never | StructuredOrInstantiable,
3723+
NotPrimitiveUnion = Any | Unknown | Enum | Void | Never | StructuredOrInstantiable,
37243724
/* @internal */
37253725
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
37263726
/* @internal */
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 #24233
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 #24233
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 #24233
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 #24233
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)