Skip to content

Commit 0997b7a

Browse files
committed
Extract logic into function
1 parent ff60341 commit 0997b7a

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
@@ -12707,38 +12707,26 @@ namespace ts {
1270712707
// We attempt to resolve the conditional type only when the check and extends types are non-generic
1270812708
if (!checkTypeInstantiable && !maybeTypeOfKind(inferredExtendsType, TypeFlags.Instantiable | TypeFlags.GenericMappedType)) {
1270912709
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
12710-
instantiationDepth--;
12711-
const result = instantiateType(root.trueType, combinedMapper || mapper);
12712-
instantiationDepth++;
12713-
return result;
12710+
return instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper);
1271412711
}
1271512712
// Return union of trueType and falseType for 'any' since it matches anything
1271612713
if (checkType.flags & TypeFlags.Any) {
12717-
instantiationDepth--;
12718-
const result = getUnionType([instantiateType(root.trueType, combinedMapper || mapper), instantiateType(root.falseType, mapper)]);
12719-
instantiationDepth++;
12720-
return result;
12714+
return getUnionType([instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper), instantiateTypeWithoutDepthIncrease(root.falseType, mapper)]);
1272112715
}
1272212716
// Return falseType for a definitely false extends check. We check an instantiations of the two
1272312717
// types with type parameters mapped to the wildcard type, the most permissive instantiations
1272412718
// possible (the wildcard type is assignable to and from all types). If those are not related,
1272512719
// then no instantiations will be and we can just return the false branch type.
1272612720
if (!isTypeAssignableTo(getPermissiveInstantiation(checkType), getPermissiveInstantiation(inferredExtendsType))) {
12727-
instantiationDepth--;
12728-
const result = instantiateType(root.falseType, mapper);
12729-
instantiationDepth++;
12730-
return result;
12721+
return instantiateTypeWithoutDepthIncrease(root.falseType, mapper);
1273112722
}
1273212723
// Return trueType for a definitely true extends check. We check instantiations of the two
1273312724
// types with type parameters mapped to their restrictive form, i.e. a form of the type parameter
1273412725
// that has no constraint. This ensures that, for example, the type
1273512726
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number
1273612727
// doesn't immediately resolve to 'string' instead of being deferred.
1273712728
if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
12738-
instantiationDepth--;
12739-
const result = instantiateType(root.trueType, combinedMapper || mapper);
12740-
instantiationDepth++;
12741-
return result;
12729+
return instantiateTypeWithoutDepthIncrease(root.trueType, combinedMapper || mapper);
1274212730
}
1274312731
}
1274412732
// Return a deferred type for a check that is neither definitely true nor definitely false
@@ -13715,6 +13703,17 @@ namespace ts {
1371513703
return result;
1371613704
}
1371713705

13706+
/**
13707+
* This can be used to avoid the penalty on instantiation depth for types which result from immediate
13708+
* simplification. It essentially removes the depth increase done in `instantiateType`.
13709+
*/
13710+
function instantiateTypeWithoutDepthIncrease(type: Type, mapper: TypeMapper | undefined) {
13711+
instantiationDepth--;
13712+
const result = instantiateType(type, mapper);
13713+
instantiationDepth++;
13714+
return result;
13715+
}
13716+
1371813717
function instantiateTypeWorker(type: Type, mapper: TypeMapper): Type {
1371913718
const flags = type.flags;
1372013719
if (flags & TypeFlags.TypeParameter) {

0 commit comments

Comments
 (0)