Merge pull request #26895 from Microsoft/callableErrors

Find first callable/constructable type in union when appropriate
This commit is contained in:
Daniel Rosenwasser
2018-09-07 00:06:11 -07:00
committed by GitHub
8 changed files with 190 additions and 11 deletions

View File

@@ -11441,7 +11441,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);
}
@@ -11472,6 +11473,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;