Skip to content

Commit b032e06

Browse files
committed
Resolve conditional type only when check and extends types are non-generic
1 parent 03a98a2 commit b032e06

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9784,31 +9784,29 @@ namespace ts {
97849784
if (checkType === wildcardType || extendsType === wildcardType) {
97859785
return wildcardType;
97869786
}
9787-
// If this is a distributive conditional type and the check type is generic we need to defer
9788-
// resolution of the conditional type such that a later instantiation will properly distribute
9789-
// over union types.
9790-
const isDeferred = root.isDistributive && maybeTypeOfKind(checkType, TypeFlags.Instantiable);
9787+
const checkTypeInstantiable = maybeTypeOfKind(checkType, TypeFlags.Instantiable);
97919788
let combinedMapper: TypeMapper | undefined;
97929789
if (root.inferTypeParameters) {
97939790
const context = createInferenceContext(root.inferTypeParameters, /*signature*/ undefined, InferenceFlags.None);
9794-
if (!isDeferred) {
9791+
if (!checkTypeInstantiable) {
97959792
// We don't want inferences from constraints as they may cause us to eagerly resolve the
97969793
// conditional type instead of deferring resolution. Also, we always want strict function
97979794
// types rules (i.e. proper contravariance) for inferences.
97989795
inferTypes(context.inferences, checkType, extendsType, InferencePriority.NoConstraints | InferencePriority.AlwaysStrict);
97999796
}
98009797
combinedMapper = combineTypeMappers(mapper, context);
98019798
}
9802-
if (!isDeferred) {
9803-
if (extendsType.flags & TypeFlags.AnyOrUnknown) {
9799+
// Instantiate the extends type including inferences for 'infer T' type parameters
9800+
const inferredExtendsType = combinedMapper ? instantiateType(root.extendsType, combinedMapper) : extendsType;
9801+
// We attempt to resolve the conditional type only when the check and extends types are non-generic
9802+
if (!checkTypeInstantiable && !maybeTypeOfKind(inferredExtendsType, TypeFlags.Instantiable)) {
9803+
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
98049804
return instantiateType(root.trueType, mapper);
98059805
}
98069806
// Return union of trueType and falseType for 'any' since it matches anything
98079807
if (checkType.flags & TypeFlags.Any) {
98089808
return getUnionType([instantiateType(root.trueType, combinedMapper || mapper), instantiateType(root.falseType, mapper)]);
98099809
}
9810-
// Instantiate the extends type including inferences for 'infer T' type parameters
9811-
const inferredExtendsType = combinedMapper ? instantiateType(root.extendsType, combinedMapper) : extendsType;
98129810
// Return falseType for a definitely false extends check. We check an instantations of the two
98139811
// types with type parameters mapped to the wildcard type, the most permissive instantiations
98149812
// possible (the wildcard type is assignable to and from all types). If those are not related,

0 commit comments

Comments
 (0)