Only instantiate types if we need to

This commit is contained in:
Anders Hejlsberg 2019-04-29 17:17:16 -07:00
parent 19bdaf8644
commit 31551fd0ae

View file

@ -10383,11 +10383,11 @@ namespace ts {
// We attempt to resolve the conditional type only when the check and extends types are non-generic // We attempt to resolve the conditional type only when the check and extends types are non-generic
if (!checkTypeInstantiable && !maybeTypeOfKind(inferredExtendsType, TypeFlags.Instantiable | TypeFlags.GenericMappedType)) { if (!checkTypeInstantiable && !maybeTypeOfKind(inferredExtendsType, TypeFlags.Instantiable | TypeFlags.GenericMappedType)) {
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) { if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
return instantiateType(root.trueType, combinedMapper || mapper); return combinedMapper ? instantiateType(root.trueType, combinedMapper) : trueType;
} }
// Return union of trueType and falseType for 'any' since it matches anything // Return union of trueType and falseType for 'any' since it matches anything
if (checkType.flags & TypeFlags.Any) { if (checkType.flags & TypeFlags.Any) {
return getUnionType([instantiateType(root.trueType, combinedMapper || mapper), falseType]); return getUnionType([combinedMapper ? instantiateType(root.trueType, combinedMapper) : trueType, falseType]);
} }
// Return falseType for a definitely false extends check. We check an instantiations of the two // Return falseType for a definitely false extends check. We check an instantiations of the two
// types with type parameters mapped to the wildcard type, the most permissive instantiations // types with type parameters mapped to the wildcard type, the most permissive instantiations
@ -10402,7 +10402,7 @@ namespace ts {
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number // type Foo<T extends { x: any }> = T extends { x: string } ? string : number
// doesn't immediately resolve to 'string' instead of being deferred. // doesn't immediately resolve to 'string' instead of being deferred.
if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) { if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
return instantiateType(root.trueType, combinedMapper || mapper); return combinedMapper ? instantiateType(root.trueType, combinedMapper) : trueType;
} }
} }
// Return a deferred type for a check that is neither definitely true nor definitely false // Return a deferred type for a check that is neither definitely true nor definitely false