mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-23 07:07:09 -05:00
Don't widen when type parameter occurs at top level in type predicate (#52031)
This commit is contained in:
@@ -23465,6 +23465,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
type.flags & TypeFlags.Conditional && (getTrueTypeFromConditionalType(type as ConditionalType) === typeParameter || getFalseTypeFromConditionalType(type as ConditionalType) === typeParameter));
|
||||
}
|
||||
|
||||
function isTypeParameterAtTopLevelInReturnType(signature: Signature, typeParameter: TypeParameter) {
|
||||
const typePredicate = getTypePredicateOfSignature(signature);
|
||||
return typePredicate ? !!typePredicate.type && isTypeParameterAtTopLevel(typePredicate.type, typeParameter) :
|
||||
isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), typeParameter);
|
||||
}
|
||||
|
||||
/** Create an object with properties named in the string literal type. Every property has type `any` */
|
||||
function createEmptyObjectTypeFromStringLiteral(type: Type) {
|
||||
const members = createSymbolTable();
|
||||
@@ -24573,7 +24579,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
// the type parameter was fixed during inference or does not occur at top-level in the return type.
|
||||
const primitiveConstraint = hasPrimitiveConstraint(inference.typeParameter) || isConstTypeVariable(inference.typeParameter);
|
||||
const widenLiteralTypes = !primitiveConstraint && inference.topLevel &&
|
||||
(inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter));
|
||||
(inference.isFixed || !isTypeParameterAtTopLevelInReturnType(signature, inference.typeParameter));
|
||||
const baseCandidates = primitiveConstraint ? sameMap(candidates, getRegularTypeOfLiteralType) :
|
||||
widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) :
|
||||
candidates;
|
||||
|
||||
Reference in New Issue
Block a user