Skip to content

Commit 7181c2a

Browse files
committed
Limit simplification to single prop or partial types
1 parent 8957dfe commit 7181c2a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12526,17 +12526,23 @@ namespace ts {
1252612526
return isEmptyObjectType(type) || !!(type.flags & (TypeFlags.Null | TypeFlags.Undefined | TypeFlags.BooleanLike | TypeFlags.NumberLike | TypeFlags.BigIntLike | TypeFlags.StringLike | TypeFlags.EnumLike | TypeFlags.NonPrimitive | TypeFlags.Index));
1252712527
}
1252812528

12529+
function isSinglePropertyAnonymousObjectType(type: Type) {
12530+
return !!(type.flags & TypeFlags.Object) &&
12531+
!!(getObjectFlags(type) & ObjectFlags.Anonymous) &&
12532+
(length(getPropertiesOfType(type)) === 1 || every(getPropertiesOfType(type), p => !!(p.flags & SymbolFlags.Optional)));
12533+
}
12534+
1252912535
function tryMergeUnionOfObjectTypeAndEmptyObject(type: UnionType, readonly: boolean): Type | undefined {
1253012536
if (type.types.length === 2) {
1253112537
const firstType = type.types[0];
1253212538
const secondType = type.types[1];
1253312539
if (every(type.types, isEmptyObjectTypeOrSpreadsIntoEmptyObject)) {
1253412540
return isEmptyObjectType(firstType) ? firstType : isEmptyObjectType(secondType) ? secondType : emptyObjectType;
1253512541
}
12536-
if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(firstType)) {
12542+
if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(firstType) && isSinglePropertyAnonymousObjectType(secondType)) {
1253712543
return getAnonymousPartialType(secondType);
1253812544
}
12539-
if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(secondType)) {
12545+
if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(secondType) && isSinglePropertyAnonymousObjectType(firstType)) {
1254012546
return getAnonymousPartialType(firstType);
1254112547
}
1254212548
}

0 commit comments

Comments
 (0)