Skip to content

Commit e6a50a7

Browse files
authored
Use strict subtype relation in getCommonSupertype (#61903)
1 parent dca7170 commit e6a50a7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25340,11 +25340,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2534025340
// right is a supertype.
2534125341
const superTypeOrUnion = literalTypesWithSameBaseType(primaryTypes) ?
2534225342
getUnionType(primaryTypes) :
25343-
reduceLeft(primaryTypes, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
25343+
getSingleCommonSupertype(primaryTypes);
2534425344
// Add any nullable types that occurred in the candidates back to the result.
2534525345
return primaryTypes === types ? superTypeOrUnion : getNullableType(superTypeOrUnion, getCombinedTypeFlags(types) & TypeFlags.Nullable);
2534625346
}
2534725347

25348+
function getSingleCommonSupertype(types: Type[]) {
25349+
// First, find the leftmost type for which no type to the right is a strict supertype, and if that
25350+
// type is a strict supertype of all other candidates, return it. Otherwise, return the leftmost type
25351+
// for which no type to the right is a (regular) supertype.
25352+
const candidate = reduceLeft(types, (s, t) => isTypeStrictSubtypeOf(s, t) ? t : s)!;
25353+
return every(types, t => t === candidate || isTypeStrictSubtypeOf(t, candidate)) ?
25354+
candidate :
25355+
reduceLeft(types, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
25356+
}
25357+
2534825358
// Return the leftmost type for which no type to the right is a subtype.
2534925359
function getCommonSubtype(types: Type[]) {
2535025360
return reduceLeft(types, (s, t) => isTypeSubtypeOf(t, s) ? t : s)!;

0 commit comments

Comments
 (0)