Try finding the first type with a call/construct signature when relating to unions.

This commit is contained in:
Daniel Rosenwasser 2018-09-04 19:25:46 -07:00
parent 289ae3cca6
commit c5c594f1e7

View file

@ -11395,7 +11395,8 @@ namespace ts {
const bestMatchingType =
findMatchingDiscriminantType(source, target) ||
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
findBestTypeForObjectLiteral(source, target);
findBestTypeForObjectLiteral(source, target) ||
findBestTypeForInvokable(source, target);
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
}
@ -11426,6 +11427,15 @@ namespace ts {
}
}
function findBestTypeForInvokable(source: Type, unionTarget: UnionOrIntersectionType) {
let signatureKind = SignatureKind.Call;
const hasSignatures = getSignaturesOfType(source, signatureKind).length > 0 ||
(signatureKind = SignatureKind.Construct, getSignaturesOfType(source, signatureKind).length > 0);
if (hasSignatures) {
return find(unionTarget.types, t => getSignaturesOfType(t, signatureKind).length > 0);
}
}
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
let match: Type | undefined;