Preserve arity for preserving js optional parameters (#37173)

This commit is contained in:
Wesley Wigham
2020-03-04 00:48:53 -08:00
committed by GitHub
parent 7a82b74983
commit dfc0b58fe7
6 changed files with 77 additions and 3 deletions

View File

@@ -10510,7 +10510,7 @@ namespace ts {
const signature = getSignatureFromDeclaration(node.parent);
const parameterIndex = node.parent.parameters.indexOf(node);
Debug.assert(parameterIndex >= 0);
return parameterIndex >= getMinArgumentCount(signature);
return parameterIndex >= getMinArgumentCount(signature, /*strongArityForUntypedJS*/ true);
}
const iife = getImmediatelyInvokedFunctionExpression(node.parent);
if (iife) {
@@ -10601,6 +10601,9 @@ namespace ts {
isValueSignatureDeclaration(declaration) &&
!hasJSDocParameterTags(declaration) &&
!getJSDocType(declaration);
if (isUntypedSignatureInJSFile) {
flags |= SignatureFlags.IsUntypedSignatureInJSFile;
}
// If this is a JSDoc construct signature, then skip the first parameter in the
// parameter list. The first parameter represents the return type of the construct
@@ -10631,7 +10634,6 @@ namespace ts {
const isOptionalParameter = isOptionalJSDocParameterTag(param) ||
param.initializer || param.questionToken || param.dotDotDotToken ||
iife && parameters.length > iife.arguments.length && !type ||
isUntypedSignatureInJSFile ||
isJSDocOptionalParameter(param);
if (!isOptionalParameter) {
minArgumentCount = parameters.length;
@@ -26375,7 +26377,7 @@ namespace ts {
return length;
}
function getMinArgumentCount(signature: Signature) {
function getMinArgumentCount(signature: Signature, strongArityForUntypedJS?: boolean) {
if (signatureHasRestParameter(signature)) {
const restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
if (isTupleType(restType)) {
@@ -26385,6 +26387,9 @@ namespace ts {
}
}
}
if (!strongArityForUntypedJS && signature.flags & SignatureFlags.IsUntypedSignatureInJSFile) {
return 0;
}
return signature.minArgumentCount;
}

View File

@@ -4817,6 +4817,7 @@ namespace ts {
HasLiteralTypes = 1 << 1, // Indicates signature is specialized
IsInnerCallChain = 1 << 2, // Indicates signature comes from a CallChain nested in an outer OptionalChain
IsOuterCallChain = 1 << 3, // Indicates signature comes from a CallChain that is the outermost chain of an optional expression
IsUntypedSignatureInJSFile = 1 << 4, // Indicates signature is from a js file and has no types
// We do not propagate `IsInnerCallChain` to instantiated signatures, as that would result in us
// attempting to add `| undefined` on each recursive call to `getReturnTypeOfSignature` when