Skip to content

Commit 958017d

Browse files
committed
Avoid calculating union in spread if types are identical
1 parent 640743f commit 958017d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18176,7 +18176,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1817618176
const declarations = concatenate(leftProp.declarations, rightProp.declarations);
1817718177
const flags = SymbolFlags.Property | (leftProp.flags & SymbolFlags.Optional);
1817818178
const result = createSymbol(flags, leftProp.escapedName);
18179-
result.links.type = getUnionType([getTypeOfSymbol(leftProp), removeMissingOrUndefinedType(rightType)], UnionReduction.Subtype);
18179+
// Optimization: avoid calculating the union type if spreading into the exact same type.
18180+
// This is common, e.g. spreading one options bag into another where the bags have the
18181+
// same type, or have properties which overlap. If the unions are large, it may turn out
18182+
// to be expensive to perform subtype reduction.
18183+
const leftType = getTypeOfSymbol(leftProp);
18184+
const leftTypeWithoutUndefined = removeMissingOrUndefinedType(leftType);
18185+
const rightTypeWithoutUndefined = removeMissingOrUndefinedType(rightType);
18186+
result.links.type = leftTypeWithoutUndefined === rightTypeWithoutUndefined ? leftType : getUnionType([leftType, rightTypeWithoutUndefined], UnionReduction.Subtype);
1818018187
result.links.leftSpread = leftProp;
1818118188
result.links.rightSpread = rightProp;
1818218189
result.declarations = declarations;

0 commit comments

Comments
 (0)