Skip to content

Commit 9017e0a

Browse files
committed
Allow missing argument for IIFE parameter with no type annotation
1 parent 5b075ff commit 9017e0a

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5058,9 +5058,10 @@ namespace ts {
50585058
if (!links.resolvedSignature) {
50595059
const parameters: Symbol[] = [];
50605060
let hasLiteralTypes = false;
5061-
let minArgumentCount = -1;
5061+
let minArgumentCount = 0;
50625062
let thisParameter: Symbol = undefined;
50635063
let hasThisParameter: boolean;
5064+
const iife = getImmediatelyInvokedFunctionExpression(declaration);
50645065
const isJSConstructSignature = isJSDocConstructSignature(declaration);
50655066

50665067
// If this is a JSDoc construct signature, then skip the first parameter in the
@@ -5087,14 +5088,12 @@ namespace ts {
50875088
hasLiteralTypes = true;
50885089
}
50895090

5090-
if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) {
5091-
if (minArgumentCount < 0) {
5092-
minArgumentCount = i - (hasThisParameter ? 1 : 0);
5093-
}
5094-
}
5095-
else {
5096-
// If we see any required parameters, it means the prior ones were not in fact optional.
5097-
minArgumentCount = -1;
5091+
// Record a new minimum argument count if this is not an optional parameter
5092+
const isOptionalParameter = param.initializer || param.questionToken || param.dotDotDotToken ||
5093+
iife && parameters.length > iife.arguments.length && !param.type ||
5094+
isJSDocOptionalParameter(param);
5095+
if (!isOptionalParameter) {
5096+
minArgumentCount = parameters.length;
50985097
}
50995098
}
51005099

@@ -5109,13 +5108,6 @@ namespace ts {
51095108
}
51105109
}
51115110

5112-
if (minArgumentCount < 0) {
5113-
minArgumentCount = declaration.parameters.length - (hasThisParameter ? 1 : 0);
5114-
}
5115-
if (isJSConstructSignature) {
5116-
minArgumentCount--;
5117-
}
5118-
51195111
const classType = declaration.kind === SyntaxKind.Constructor ?
51205112
getDeclaredTypeOfClassOrInterface(getMergedSymbol((<ClassDeclaration>declaration.parent).symbol))
51215113
: undefined;
@@ -10933,23 +10925,23 @@ namespace ts {
1093310925
const func = parameter.parent;
1093410926
if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
1093510927
const iife = getImmediatelyInvokedFunctionExpression(func);
10936-
if (iife) {
10928+
if (iife && iife.arguments) {
1093710929
const indexOfParameter = indexOf(func.parameters, parameter);
10938-
if (iife.arguments && indexOfParameter < iife.arguments.length) {
10939-
if (parameter.dotDotDotToken) {
10940-
const restTypes: Type[] = [];
10941-
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
10942-
restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
10943-
}
10944-
return createArrayType(getUnionType(restTypes));
10945-
}
10946-
const links = getNodeLinks(iife);
10947-
const cached = links.resolvedSignature;
10948-
links.resolvedSignature = anySignature;
10949-
const type = getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter]));
10950-
links.resolvedSignature = cached;
10951-
return type;
10952-
}
10930+
if (parameter.dotDotDotToken) {
10931+
const restTypes: Type[] = [];
10932+
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
10933+
restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
10934+
}
10935+
return restTypes.length ? createArrayType(getUnionType(restTypes)) : undefined;
10936+
}
10937+
const links = getNodeLinks(iife);
10938+
const cached = links.resolvedSignature;
10939+
links.resolvedSignature = anySignature;
10940+
const type = indexOfParameter < iife.arguments.length ?
10941+
getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])) :
10942+
parameter.initializer ? undefined : undefinedWideningType;
10943+
links.resolvedSignature = cached;
10944+
return type;
1095310945
}
1095410946
const contextualSignature = getContextualSignature(func);
1095510947
if (contextualSignature) {

0 commit comments

Comments
 (0)