From 649f2944137b21621f3556fea8aafe0ef5f2000e Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 17 Jul 2018 21:54:33 -0700 Subject: [PATCH] Elaborate on the first non-array type when object literals are compared against 'T | T[]'. --- src/compiler/checker.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e59641bed41..88983c9eff9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11182,7 +11182,8 @@ namespace ts { if (reportErrors) { const bestMatchingType = findMatchingDiscriminantType(source, target) || - findMatchingTypeReferenceOrTypeAliasReference(source, target); + findMatchingTypeReferenceOrTypeAliasReference(source, target) || + findBestTypeForObjectLiteral(source, target); isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true); } @@ -11207,6 +11208,11 @@ namespace ts { } } + function findBestTypeForObjectLiteral(source: Type, unionTarget: UnionOrIntersectionType) { + if (getObjectFlags(source) & ObjectFlags.ObjectLiteral && forEachType(unionTarget, isArrayLikeType)) { + return find(unionTarget.types, t => !isArrayLikeType(t)); + } + } // Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {