mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-11 09:24:19 -06:00
Use strict subtype relation in getCommonSupertype (#61903)
This commit is contained in:
parent
dca71700ba
commit
e6a50a7861
@ -25340,11 +25340,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
// right is a supertype.
|
||||
const superTypeOrUnion = literalTypesWithSameBaseType(primaryTypes) ?
|
||||
getUnionType(primaryTypes) :
|
||||
reduceLeft(primaryTypes, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
|
||||
getSingleCommonSupertype(primaryTypes);
|
||||
// Add any nullable types that occurred in the candidates back to the result.
|
||||
return primaryTypes === types ? superTypeOrUnion : getNullableType(superTypeOrUnion, getCombinedTypeFlags(types) & TypeFlags.Nullable);
|
||||
}
|
||||
|
||||
function getSingleCommonSupertype(types: Type[]) {
|
||||
// First, find the leftmost type for which no type to the right is a strict supertype, and if that
|
||||
// type is a strict supertype of all other candidates, return it. Otherwise, return the leftmost type
|
||||
// for which no type to the right is a (regular) supertype.
|
||||
const candidate = reduceLeft(types, (s, t) => isTypeStrictSubtypeOf(s, t) ? t : s)!;
|
||||
return every(types, t => t === candidate || isTypeStrictSubtypeOf(t, candidate)) ?
|
||||
candidate :
|
||||
reduceLeft(types, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
|
||||
}
|
||||
|
||||
// Return the leftmost type for which no type to the right is a subtype.
|
||||
function getCommonSubtype(types: Type[]) {
|
||||
return reduceLeft(types, (s, t) => isTypeSubtypeOf(t, s) ? t : s)!;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user