@@ -25317,7 +25317,7 @@ namespace ts {
25317
25317
return !!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable));
25318
25318
}
25319
25319
25320
- function hasContextualTypeWithNoGenericTypes (node: Node, checkMode: CheckMode | undefined) {
25320
+ function tryGetContextualTypeWithNoGenericTypes (node: Node, checkMode: CheckMode | undefined) {
25321
25321
// Computing the contextual type for a child of a JSX element involves resolving the type of the
25322
25322
// element's tag name, so we exclude that here to avoid circularities.
25323
25323
// If check mode has `CheckMode.RestBindingElement`, we skip binding pattern contextual types,
@@ -25327,7 +25327,7 @@ namespace ts {
25327
25327
(checkMode && checkMode & CheckMode.RestBindingElement ?
25328
25328
getContextualType(node, ContextFlags.SkipBindingPatterns)
25329
25329
: getContextualType(node));
25330
- return contextualType && !isGenericType(contextualType);
25330
+ return contextualType && !isGenericType(contextualType) ? contextualType : undefined ;
25331
25331
}
25332
25332
25333
25333
function getNarrowableTypeForReference(type: Type, reference: Node, checkMode?: CheckMode) {
@@ -25338,9 +25338,13 @@ namespace ts {
25338
25338
// control flow analysis an opportunity to narrow it further. For example, for a reference of a type
25339
25339
// parameter type 'T extends string | undefined' with a contextual type 'string', we substitute
25340
25340
// 'string | undefined' to give control flow analysis the opportunity to narrow to type 'string'.
25341
+ let contextualType: Type | undefined;
25341
25342
const substituteConstraints = !(checkMode && checkMode & CheckMode.Inferential) &&
25342
- someType(type, isGenericTypeWithUnionConstraint) &&
25343
- (isConstraintPosition(type, reference) || hasContextualTypeWithNoGenericTypes(reference, checkMode));
25343
+ (isConstraintPosition(type, reference) || (contextualType = tryGetContextualTypeWithNoGenericTypes(reference, checkMode))) &&
25344
+ (
25345
+ someType(type, isGenericTypeWithUnionConstraint) ||
25346
+ ((type.flags & TypeFlags.Instantiable) && contextualType && isEmptyObjectType(contextualType))
25347
+ );
25344
25348
return substituteConstraints ? mapType(type, t => t.flags & TypeFlags.Instantiable ? getBaseConstraintOrType(t) : t) : type;
25345
25349
}
25346
25350
0 commit comments