mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-20 22:51:17 -05:00
Merge pull request #11905 from Microsoft/skip-overloads-with-too-short-function-parameters
Skip overloads with too short function parameters
This commit is contained in:
@@ -10328,16 +10328,32 @@ namespace ts {
|
||||
|
||||
// If the given type is an object or union type, if that type has a single signature, and if
|
||||
// that signature is non-generic, return the signature. Otherwise return undefined.
|
||||
function getNonGenericSignature(type: Type): Signature {
|
||||
function getNonGenericSignature(type: Type, node: FunctionExpression | ArrowFunction | MethodDeclaration): Signature {
|
||||
const signatures = getSignaturesOfStructuredType(type, SignatureKind.Call);
|
||||
if (signatures.length === 1) {
|
||||
const signature = signatures[0];
|
||||
if (!signature.typeParameters) {
|
||||
if (!signature.typeParameters && !isAritySmaller(signature, node)) {
|
||||
return signature;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** If the contextual signature has fewer parameters than the function expression, do not use it */
|
||||
function isAritySmaller(signature: Signature, target: FunctionExpression | ArrowFunction | MethodDeclaration) {
|
||||
let targetParameterCount = 0;
|
||||
for (; targetParameterCount < target.parameters.length; targetParameterCount++) {
|
||||
const param = target.parameters[targetParameterCount];
|
||||
if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (target.parameters.length && parameterIsThisKeyword(target.parameters[0])) {
|
||||
targetParameterCount--;
|
||||
}
|
||||
const sourceLength = signature.hasRestParameter ? Number.MAX_VALUE : signature.parameters.length;
|
||||
return sourceLength < targetParameterCount;
|
||||
}
|
||||
|
||||
function isFunctionExpressionOrArrowFunction(node: Node): node is FunctionExpression | ArrowFunction {
|
||||
return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction;
|
||||
}
|
||||
@@ -10367,12 +10383,12 @@ namespace ts {
|
||||
return undefined;
|
||||
}
|
||||
if (!(type.flags & TypeFlags.Union)) {
|
||||
return getNonGenericSignature(type);
|
||||
return getNonGenericSignature(type, node);
|
||||
}
|
||||
let signatureList: Signature[];
|
||||
const types = (<UnionType>type).types;
|
||||
for (const current of types) {
|
||||
const signature = getNonGenericSignature(current);
|
||||
const signature = getNonGenericSignature(current, node);
|
||||
if (signature) {
|
||||
if (!signatureList) {
|
||||
// This signature will contribute to contextual union signature
|
||||
|
||||
Reference in New Issue
Block a user