Skip to content

Commit c5c594f

Browse files
Try finding the first type with a call/construct signature when relating to unions.
1 parent 289ae3c commit c5c594f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11395,7 +11395,8 @@ namespace ts {
1139511395
const bestMatchingType =
1139611396
findMatchingDiscriminantType(source, target) ||
1139711397
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
11398-
findBestTypeForObjectLiteral(source, target);
11398+
findBestTypeForObjectLiteral(source, target) ||
11399+
findBestTypeForInvokable(source, target);
1139911400

1140011401
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
1140111402
}
@@ -11426,6 +11427,15 @@ namespace ts {
1142611427
}
1142711428
}
1142811429

11430+
function findBestTypeForInvokable(source: Type, unionTarget: UnionOrIntersectionType) {
11431+
let signatureKind = SignatureKind.Call;
11432+
const hasSignatures = getSignaturesOfType(source, signatureKind).length > 0 ||
11433+
(signatureKind = SignatureKind.Construct, getSignaturesOfType(source, signatureKind).length > 0);
11434+
if (hasSignatures) {
11435+
return find(unionTarget.types, t => getSignaturesOfType(t, signatureKind).length > 0);
11436+
}
11437+
}
11438+
1142911439
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
1143011440
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
1143111441
let match: Type | undefined;

0 commit comments

Comments
 (0)