Don't widen when type parameter occurs at top level in type predicate (#52031)

This commit is contained in:
Anders Hejlsberg
2023-01-12 11:18:36 -10:00
committed by GitHub
parent 5b4a8d4134
commit 59e4e38d08
5 changed files with 144 additions and 1 deletions

View File

@@ -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;