@@ -12767,26 +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- return instantiateType (root.trueType, combinedMapper || mapper);
12770+ return instantiateTypeWithoutDepthIncrease (root.trueType, combinedMapper || mapper);
1277112771 }
1277212772 // Return union of trueType and falseType for 'any' since it matches anything
1277312773 if (checkType.flags & TypeFlags.Any) {
12774- return getUnionType([instantiateType (root.trueType, combinedMapper || mapper), instantiateType (root.falseType, mapper)]);
12774+ return getUnionType([instantiateTypeWithoutDepthIncrease (root.trueType, combinedMapper || mapper), instantiateTypeWithoutDepthIncrease (root.falseType, mapper)]);
1277512775 }
1277612776 // Return falseType for a definitely false extends check. We check an instantiations of the two
1277712777 // types with type parameters mapped to the wildcard type, the most permissive instantiations
1277812778 // possible (the wildcard type is assignable to and from all types). If those are not related,
1277912779 // then no instantiations will be and we can just return the false branch type.
1278012780 if (!isTypeAssignableTo(getPermissiveInstantiation(checkType), getPermissiveInstantiation(inferredExtendsType))) {
12781- return instantiateType (root.falseType, mapper);
12781+ return instantiateTypeWithoutDepthIncrease (root.falseType, mapper);
1278212782 }
1278312783 // Return trueType for a definitely true extends check. We check instantiations of the two
1278412784 // types with type parameters mapped to their restrictive form, i.e. a form of the type parameter
1278512785 // that has no constraint. This ensures that, for example, the type
1278612786 // type Foo<T extends { x: any }> = T extends { x: string } ? string : number
1278712787 // doesn't immediately resolve to 'string' instead of being deferred.
1278812788 if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
12789- return instantiateType (root.trueType, combinedMapper || mapper);
12789+ return instantiateTypeWithoutDepthIncrease (root.trueType, combinedMapper || mapper);
1279012790 }
1279112791 }
1279212792 // Return a deferred type for a check that is neither definitely true nor definitely false
@@ -13771,6 +13771,17 @@ namespace ts {
1377113771 return result;
1377213772 }
1377313773
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+
1377413785 function instantiateTypeWorker(type: Type, mapper: TypeMapper): Type {
1377513786 const flags = type.flags;
1377613787 if (flags & TypeFlags.TypeParameter) {
0 commit comments