@@ -25340,11 +25340,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
25340
25340
// right is a supertype.
25341
25341
const superTypeOrUnion = literalTypesWithSameBaseType(primaryTypes) ?
25342
25342
getUnionType(primaryTypes) :
25343
- reduceLeft (primaryTypes, (s, t) => isTypeSubtypeOf(s, t) ? t : s)! ;
25343
+ getSingleCommonSupertype (primaryTypes) ;
25344
25344
// Add any nullable types that occurred in the candidates back to the result.
25345
25345
return primaryTypes === types ? superTypeOrUnion : getNullableType(superTypeOrUnion, getCombinedTypeFlags(types) & TypeFlags.Nullable);
25346
25346
}
25347
25347
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
+
25348
25358
// Return the leftmost type for which no type to the right is a subtype.
25349
25359
function getCommonSubtype(types: Type[]) {
25350
25360
return reduceLeft(types, (s, t) => isTypeSubtypeOf(t, s) ? t : s)!;
0 commit comments