Skip to content

Commit 649f294

Browse files
Elaborate on the first non-array type when object literals are compared against 'T | T[]'.
1 parent 36e26c1 commit 649f294

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11182,7 +11182,8 @@ namespace ts {
1118211182
if (reportErrors) {
1118311183
const bestMatchingType =
1118411184
findMatchingDiscriminantType(source, target) ||
11185-
findMatchingTypeReferenceOrTypeAliasReference(source, target);
11185+
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
11186+
findBestTypeForObjectLiteral(source, target);
1118611187

1118711188
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
1118811189
}
@@ -11207,6 +11208,11 @@ namespace ts {
1120711208
}
1120811209
}
1120911210

11211+
function findBestTypeForObjectLiteral(source: Type, unionTarget: UnionOrIntersectionType) {
11212+
if (getObjectFlags(source) & ObjectFlags.ObjectLiteral && forEachType(unionTarget, isArrayLikeType)) {
11213+
return find(unionTarget.types, t => !isArrayLikeType(t));
11214+
}
11215+
}
1121011216

1121111217
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
1121211218
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {

0 commit comments

Comments
 (0)