Skip to content

Commit 8931791

Browse files
committed
Allow deeply nested immediately resolving conditionals without any syntactic requirements or implementation contortions
Extract logic into function
1 parent 3712faa commit 8931791

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12719,7 +12719,7 @@ namespace ts {
1271912719
if (!(inferredExtendsType.flags & TypeFlags.AnyOrUnknown) && (checkType.flags & TypeFlags.Any || !isTypeAssignableTo(getPermissiveInstantiation(checkType), getPermissiveInstantiation(inferredExtendsType)))) {
1272012720
// Return union of trueType and falseType for 'any' since it matches anything
1272112721
if (checkType.flags & TypeFlags.Any) {
12722-
(extraTypes || (extraTypes = [])).push(instantiateType(root.trueType, combinedMapper || mapper));
12722+
(extraTypes || (extraTypes = [])).push(instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper));
1272312723
}
1272412724
// If falseType is an immediately nested conditional type that isn't distributive or has an
1272512725
// identical checkType, switch to that type and loop.
@@ -12731,7 +12731,7 @@ namespace ts {
1273112731
continue;
1273212732
}
1273312733
}
12734-
result = instantiateType(falseType, mapper);
12734+
result = instantiateTypeWithoutDepthIncrease(falseType, mapper);
1273512735
break;
1273612736
}
1273712737
// Return trueType for a definitely true extends check. We check instantiations of the two
@@ -12740,7 +12740,7 @@ namespace ts {
1274012740
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number
1274112741
// doesn't immediately resolve to 'string' instead of being deferred.
1274212742
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown || isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
12743-
result = instantiateType(root.trueType, combinedMapper || mapper);
12743+
result = instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper);
1274412744
break;
1274512745
}
1274612746
}
@@ -13720,6 +13720,17 @@ namespace ts {
1372013720
return result;
1372113721
}
1372213722

13723+
/**
13724+
* This can be used to avoid the penalty on instantiation depth for types which result from immediate
13725+
* simplification. It essentially removes the depth increase done in `instantiateType`.
13726+
*/
13727+
function instantiateTypeWithoutDepthIncrease(type: Type, mapper: TypeMapper | undefined) {
13728+
instantiationDepth--;
13729+
const result = instantiateType(type, mapper);
13730+
instantiationDepth++;
13731+
return result;
13732+
}
13733+
1372313734
function instantiateTypeWorker(type: Type, mapper: TypeMapper): Type {
1372413735
const flags = type.flags;
1372513736
if (flags & TypeFlags.TypeParameter) {

0 commit comments

Comments
 (0)