Skip to content

Commit 87eb951

Browse files
committed
Extract logic into function
1 parent ea5ade3 commit 87eb951

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12767,38 +12767,26 @@ namespace ts {
1276712767
// We attempt to resolve the conditional type only when the check and extends types are non-generic
1276812768
if (!checkTypeInstantiable && !isGenericObjectType(inferredExtendsType) && !isGenericIndexType(inferredExtendsType)) {
1276912769
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
12770-
instantiationDepth--;
12771-
const result = instantiateType(root.trueType, combinedMapper || mapper);
12772-
instantiationDepth++;
12773-
return result;
12770+
return instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper);
1277412771
}
1277512772
// Return union of trueType and falseType for 'any' since it matches anything
1277612773
if (checkType.flags & TypeFlags.Any) {
12777-
instantiationDepth--;
12778-
const result = getUnionType([instantiateType(root.trueType, combinedMapper || mapper), instantiateType(root.falseType, mapper)]);
12779-
instantiationDepth++;
12780-
return result;
12774+
return getUnionType([instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper), instantiateTypeWithoutDepthIncrease(root.falseType, mapper)]);
1278112775
}
1278212776
// Return falseType for a definitely false extends check. We check an instantiations of the two
1278312777
// types with type parameters mapped to the wildcard type, the most permissive instantiations
1278412778
// possible (the wildcard type is assignable to and from all types). If those are not related,
1278512779
// then no instantiations will be and we can just return the false branch type.
1278612780
if (!isTypeAssignableTo(getPermissiveInstantiation(checkType), getPermissiveInstantiation(inferredExtendsType))) {
12787-
instantiationDepth--;
12788-
const result = instantiateType(root.falseType, mapper);
12789-
instantiationDepth++;
12790-
return result;
12781+
return instantiateTypeWithoutDepthIncrease(root.falseType, mapper);
1279112782
}
1279212783
// Return trueType for a definitely true extends check. We check instantiations of the two
1279312784
// types with type parameters mapped to their restrictive form, i.e. a form of the type parameter
1279412785
// that has no constraint. This ensures that, for example, the type
1279512786
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number
1279612787
// doesn't immediately resolve to 'string' instead of being deferred.
1279712788
if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
12798-
instantiationDepth--;
12799-
const result = instantiateType(root.trueType, combinedMapper || mapper);
12800-
instantiationDepth++;
12801-
return result;
12789+
return instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper);
1280212790
}
1280312791
}
1280412792
// Return a deferred type for a check that is neither definitely true nor definitely false
@@ -13783,6 +13771,17 @@ namespace ts {
1378313771
return result;
1378413772
}
1378513773

13774+
/**
13775+
* This can be used to avoid the penalty on instantiation depth for types which result from immediate
13776+
* simplification. It essentially removes the depth increase done in `instantiateType`.
13777+
*/
13778+
function instantiateTypeWithoutDepthIncrease(type: Type, mapper: TypeMapper | undefined) {
13779+
instantiationDepth--;
13780+
const result = instantiateType(type, mapper);
13781+
instantiationDepth++;
13782+
return result;
13783+
}
13784+
1378613785
function instantiateTypeWorker(type: Type, mapper: TypeMapper): Type {
1378713786
const flags = type.flags;
1378813787
if (flags & TypeFlags.TypeParameter) {

0 commit comments

Comments
 (0)