Skip to content

Commit 1b96117

Browse files
committed
Avoid producing brand new types in getCommonSupertype
1 parent 30d0cdc commit 1b96117

File tree

2 files changed

+12
-48
lines changed

2 files changed

+12
-48
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21218,29 +21218,26 @@ namespace ts {
2121821218
}
2121921219
return literalTypesWithSameBaseType(types) ?
2122021220
getUnionType(types) :
21221-
reduceLeft(types, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
21221+
reduceLeft(types, (s, t) => isTypeSubtypeOf(getNonNullableTypeIfNeeded(s), getNonNullableTypeIfNeeded(t)) ? t : s)!;
2122221222
}
2122321223

2122421224
function getCommonSupertype(types: Type[]): Type {
2122521225
if (!strictNullChecks) {
2122621226
return getSupertypeOrUnion(types);
2122721227
}
21228-
const primaryTypes = filter(types, t => !(t.flags & TypeFlags.Nullable));
21229-
if (primaryTypes.length) {
21230-
const supertypeOrUnion = getSupertypeOrUnion(primaryTypes);
21231-
const supertypeOrUnionFacts = getTypeFacts(supertypeOrUnion);
21232-
const allFacts = getAllTypeFacts(types);
2123321228

21234-
let missingNullableFlags: TypeFlags = 0;
21235-
if (allFacts & TypeFacts.IsNull && !(supertypeOrUnionFacts & TypeFacts.IsNull)) {
21236-
missingNullableFlags |= TypeFlags.Null;
21237-
}
21238-
if (allFacts & TypeFacts.IsUndefined && !(supertypeOrUnionFacts & TypeFacts.IsUndefined)) {
21239-
missingNullableFlags |= TypeFlags.Undefined;
21240-
}
21241-
return getNullableType(supertypeOrUnion, missingNullableFlags);
21229+
const supertypeOrUnion = getSupertypeOrUnion(types);
21230+
const supertypeOrUnionFacts = getTypeFacts(supertypeOrUnion);
21231+
const allFacts = getAllTypeFacts(types);
21232+
21233+
let missingNullableFlags: TypeFlags = 0;
21234+
if (allFacts & TypeFacts.IsNull && !(supertypeOrUnionFacts & TypeFacts.IsNull)) {
21235+
missingNullableFlags |= TypeFlags.Null;
21236+
}
21237+
if (allFacts & TypeFacts.IsUndefined && !(supertypeOrUnionFacts & TypeFacts.IsUndefined)) {
21238+
missingNullableFlags |= TypeFlags.Undefined;
2124221239
}
21243-
return getUnionType(types, UnionReduction.Subtype);
21240+
return getNullableType(supertypeOrUnion, missingNullableFlags);
2124421241
}
2124521242

2124621243
// Return the leftmost type for which no type to the right is a subtype.

tests/baselines/reference/inferenceOfNullableObjectTypesWithCommonBase.errors.txt

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)