Skip to content

Commit 865c120

Browse files
authored
Use couldContainTypeVariables to short-circuit instantiateType (#36951)
This is particularly impactful for large unions of string literals.
1 parent 6d1361c commit 865c120

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13798,15 +13798,17 @@ namespace ts {
1379813798
}
1379913799
return type;
1380013800
}
13801-
if (flags & TypeFlags.Union && !(flags & TypeFlags.Primitive)) {
13802-
const types = (<UnionType>type).types;
13803-
const newTypes = instantiateTypes(types, mapper);
13804-
return newTypes !== types ? getUnionType(newTypes, UnionReduction.Literal, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper)) : type;
13805-
}
13806-
if (flags & TypeFlags.Intersection) {
13807-
const types = (<IntersectionType>type).types;
13801+
if ((flags & TypeFlags.Intersection) || (flags & TypeFlags.Union && !(flags & TypeFlags.Primitive))) {
13802+
if (!couldContainTypeVariables(type)) {
13803+
return type;
13804+
}
13805+
const types = (<UnionOrIntersectionType>type).types;
1380813806
const newTypes = instantiateTypes(types, mapper);
13809-
return newTypes !== types ? getIntersectionType(newTypes, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper)) : type;
13807+
return newTypes === types
13808+
? type
13809+
: (flags & TypeFlags.Intersection)
13810+
? getIntersectionType(newTypes, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper))
13811+
: getUnionType(newTypes, UnionReduction.Literal, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
1381013812
}
1381113813
if (flags & TypeFlags.Index) {
1381213814
return getIndexType(instantiateType((<IndexType>type).type, mapper));

0 commit comments

Comments
 (0)