From 7181c2af0922bf5d4f343abb5f9d2977487a5989 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 1 Nov 2019 15:17:40 -0700 Subject: [PATCH] Limit simplification to single prop or partial types --- src/compiler/checker.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5959ba3c3d520..e9e6877a0dddf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12526,6 +12526,12 @@ namespace ts { return isEmptyObjectType(type) || !!(type.flags & (TypeFlags.Null | TypeFlags.Undefined | TypeFlags.BooleanLike | TypeFlags.NumberLike | TypeFlags.BigIntLike | TypeFlags.StringLike | TypeFlags.EnumLike | TypeFlags.NonPrimitive | TypeFlags.Index)); } + function isSinglePropertyAnonymousObjectType(type: Type) { + return !!(type.flags & TypeFlags.Object) && + !!(getObjectFlags(type) & ObjectFlags.Anonymous) && + (length(getPropertiesOfType(type)) === 1 || every(getPropertiesOfType(type), p => !!(p.flags & SymbolFlags.Optional))); + } + function tryMergeUnionOfObjectTypeAndEmptyObject(type: UnionType, readonly: boolean): Type | undefined { if (type.types.length === 2) { const firstType = type.types[0]; @@ -12533,10 +12539,10 @@ namespace ts { if (every(type.types, isEmptyObjectTypeOrSpreadsIntoEmptyObject)) { return isEmptyObjectType(firstType) ? firstType : isEmptyObjectType(secondType) ? secondType : emptyObjectType; } - if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(firstType)) { + if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(firstType) && isSinglePropertyAnonymousObjectType(secondType)) { return getAnonymousPartialType(secondType); } - if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(secondType)) { + if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(secondType) && isSinglePropertyAnonymousObjectType(firstType)) { return getAnonymousPartialType(firstType); } }