From 9c27c56779b10b62e93f1c54850cb8130555d9db Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 27 Oct 2017 10:05:44 -0700 Subject: [PATCH] Perform subtype reduction when widened union type contains {} --- src/compiler/checker.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d1ad5f6f732..c9b43c5626c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10551,7 +10551,11 @@ namespace ts { } if (type.flags & TypeFlags.Union) { const unionContext = context || createWideningContext(/*parent*/ undefined, /*propertyName*/ undefined, (type).types); - return getUnionType(sameMap((type).types, t => t.flags & TypeFlags.Nullable ? t : getWidenedTypeWithContext(t, unionContext))); + const widenedTypes = sameMap((type).types, t => t.flags & TypeFlags.Nullable ? t : getWidenedTypeWithContext(t, unionContext)); + // Widening an empty object literal transitions from a highly restrictive type to + // a highly inclusive one. For that reason we perform subtype reduction here if the + // union includes empty object types (e.g. reducing {} | string to just {}). + return getUnionType(widenedTypes, some(widenedTypes, isEmptyObjectType)); } if (isArrayType(type) || isTupleType(type)) { return createTypeReference((type).target, sameMap((type).typeArguments, getWidenedType));