@@ -21198,39 +21198,35 @@ namespace ts {
21198
21198
function literalTypesWithSameBaseType(types: Type[]): boolean {
21199
21199
let commonBaseType: Type | undefined;
21200
21200
for (const t of types) {
21201
- const baseType = getBaseTypeOfLiteralType(t);
21202
- if (!commonBaseType) {
21203
- commonBaseType = baseType;
21204
- }
21205
- if (baseType === t || baseType !== commonBaseType) {
21206
- return false;
21201
+ if (!(t.flags & TypeFlags.Never)) {
21202
+ const baseType = getBaseTypeOfLiteralType(t);
21203
+ commonBaseType ?? = baseType;
21204
+ if (baseType === t || baseType !== commonBaseType) {
21205
+ return false;
21206
+ }
21207
21207
}
21208
21208
}
21209
21209
return true;
21210
21210
}
21211
21211
21212
- // When the candidate types are all literal types with the same base type, return a union
21213
- // of those literal types. Otherwise, return the leftmost type for which no type to the
21214
- // right is a supertype.
21215
- function getSupertypeOrUnion(types: Type[]): Type {
21216
- if (types.length === 1) {
21217
- return types[0];
21218
- }
21219
- return literalTypesWithSameBaseType(types) ?
21220
- getUnionType(types) :
21221
- reduceLeft(types, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
21212
+ function getCombinedTypeFlags(types: Type[]): TypeFlags {
21213
+ return reduceLeft(types, (flags, t) => flags | (t.flags & TypeFlags.Union ? getCombinedTypeFlags((t as UnionType).types) : t.flags), 0);
21222
21214
}
21223
21215
21224
21216
function getCommonSupertype(types: Type[]): Type {
21225
- if (!strictNullChecks) {
21226
- return getSupertypeOrUnion(types);
21227
- }
21228
- const primaryTypes = filter(types, t => !(t.flags & TypeFlags.Nullable));
21229
- if (primaryTypes.length) {
21230
- const supertypeOrUnion = getSupertypeOrUnion(primaryTypes);
21231
- return primaryTypes === types ? supertypeOrUnion : getUnionType([supertypeOrUnion, ...filter(types, t => !!(t.flags & TypeFlags.Nullable))]);
21217
+ if (types.length === 1) {
21218
+ return types[0];
21232
21219
}
21233
- return getUnionType(types, UnionReduction.Subtype);
21220
+ // Remove nullable types from each of the candidates.
21221
+ const primaryTypes = strictNullChecks ? sameMap(types, t => filterType(t, u => !(u.flags & TypeFlags.Nullable))) : types;
21222
+ // When the candidate types are all literal types with the same base type, return a union
21223
+ // of those literal types. Otherwise, return the leftmost type for which no type to the
21224
+ // right is a supertype.
21225
+ const superTypeOrUnion = literalTypesWithSameBaseType(primaryTypes) ?
21226
+ getUnionType(primaryTypes) :
21227
+ reduceLeft(primaryTypes, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
21228
+ // Add any nullable types that occurred in the candidates back to the result.
21229
+ return primaryTypes === types ? superTypeOrUnion : getNullableType(superTypeOrUnion, getCombinedTypeFlags(types) & TypeFlags.Nullable);
21234
21230
}
21235
21231
21236
21232
// Return the leftmost type for which no type to the right is a subtype.
0 commit comments