mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 15:45:27 -05:00
Merge pull request #13358 from Microsoft/iifeMissingArguments
Infer optional types for IIFE parameters with missing arguments
This commit is contained in:
@@ -5058,9 +5058,10 @@ namespace ts {
|
||||
if (!links.resolvedSignature) {
|
||||
const parameters: Symbol[] = [];
|
||||
let hasLiteralTypes = false;
|
||||
let minArgumentCount = -1;
|
||||
let minArgumentCount = 0;
|
||||
let thisParameter: Symbol = undefined;
|
||||
let hasThisParameter: boolean;
|
||||
const iife = getImmediatelyInvokedFunctionExpression(declaration);
|
||||
const isJSConstructSignature = isJSDocConstructSignature(declaration);
|
||||
|
||||
// If this is a JSDoc construct signature, then skip the first parameter in the
|
||||
@@ -5087,14 +5088,12 @@ namespace ts {
|
||||
hasLiteralTypes = true;
|
||||
}
|
||||
|
||||
if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) {
|
||||
if (minArgumentCount < 0) {
|
||||
minArgumentCount = i - (hasThisParameter ? 1 : 0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If we see any required parameters, it means the prior ones were not in fact optional.
|
||||
minArgumentCount = -1;
|
||||
// Record a new minimum argument count if this is not an optional parameter
|
||||
const isOptionalParameter = param.initializer || param.questionToken || param.dotDotDotToken ||
|
||||
iife && parameters.length > iife.arguments.length && !param.type ||
|
||||
isJSDocOptionalParameter(param);
|
||||
if (!isOptionalParameter) {
|
||||
minArgumentCount = parameters.length;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5109,13 +5108,6 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
if (minArgumentCount < 0) {
|
||||
minArgumentCount = declaration.parameters.length - (hasThisParameter ? 1 : 0);
|
||||
}
|
||||
if (isJSConstructSignature) {
|
||||
minArgumentCount--;
|
||||
}
|
||||
|
||||
const classType = declaration.kind === SyntaxKind.Constructor ?
|
||||
getDeclaredTypeOfClassOrInterface(getMergedSymbol((<ClassDeclaration>declaration.parent).symbol))
|
||||
: undefined;
|
||||
@@ -10935,23 +10927,23 @@ namespace ts {
|
||||
const func = parameter.parent;
|
||||
if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
|
||||
const iife = getImmediatelyInvokedFunctionExpression(func);
|
||||
if (iife) {
|
||||
if (iife && iife.arguments) {
|
||||
const indexOfParameter = indexOf(func.parameters, parameter);
|
||||
if (iife.arguments && indexOfParameter < iife.arguments.length) {
|
||||
if (parameter.dotDotDotToken) {
|
||||
const restTypes: Type[] = [];
|
||||
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
|
||||
restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
|
||||
}
|
||||
return createArrayType(getUnionType(restTypes));
|
||||
if (parameter.dotDotDotToken) {
|
||||
const restTypes: Type[] = [];
|
||||
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
|
||||
restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
|
||||
}
|
||||
const links = getNodeLinks(iife);
|
||||
const cached = links.resolvedSignature;
|
||||
links.resolvedSignature = anySignature;
|
||||
const type = getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter]));
|
||||
links.resolvedSignature = cached;
|
||||
return type;
|
||||
return restTypes.length ? createArrayType(getUnionType(restTypes)) : undefined;
|
||||
}
|
||||
const links = getNodeLinks(iife);
|
||||
const cached = links.resolvedSignature;
|
||||
links.resolvedSignature = anySignature;
|
||||
const type = indexOfParameter < iife.arguments.length ?
|
||||
getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])) :
|
||||
parameter.initializer ? undefined : undefinedWideningType;
|
||||
links.resolvedSignature = cached;
|
||||
return type;
|
||||
}
|
||||
const contextualSignature = getContextualSignature(func);
|
||||
if (contextualSignature) {
|
||||
|
||||
Reference in New Issue
Block a user