Skip to content

Commit 8657dc1

Browse files
committed
Error on excess spread arguments
Make the *technically* correct construction illegal: ```ts declare function f(n: number): void; declare var ns: number[]; f(1, ...ns); ``` This call only makes sense if `ns = []`, but in that case, why pass `ns` at all? Allowing this call masks other errors when functions are refactored to have fewer parameters, or to stop using rest parameters: ```ts declare function old(...ns: number[]): void; declare function new(ns: number | number[]): void; old(1, ...ns); // Fine! new(1, ...ns); // Should error! ``` This change the error for excess spread arguments to be more understandable: "Expected 3 arguments, but got least 4". Previously the error would have been "Expected 3 argument, but got at least 3", which is, again, technically correct, but not understandable.
1 parent d49491b commit 8657dc1

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/compiler/checker.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15790,10 +15790,10 @@ namespace ts {
1579015790
return false;
1579115791
}
1579215792

15793-
// If spread arguments are present, check that they correspond to a rest parameter. If so, no
15794-
// further checking is necessary.
15793+
// If a spread argument is present, check that it corresponds to a rest parameter or at least that it's in the valid range.
1579515794
if (spreadArgIndex >= 0) {
15796-
return isRestParameterIndex(signature, spreadArgIndex) || spreadArgIndex >= signature.minArgumentCount;
15795+
return isRestParameterIndex(signature, spreadArgIndex) ||
15796+
signature.minArgumentCount <= spreadArgIndex && spreadArgIndex < signature.parameters.length;
1579715797
}
1579815798

1579915799
// Too many arguments implies incorrect arity.
@@ -16501,7 +16501,10 @@ namespace ts {
1650116501
const paramCount = hasRestParameter ? min :
1650216502
min < max ? min + "-" + max :
1650316503
min;
16504-
const argCount = args.length - (hasSpreadArgument ? 1 : 0);
16504+
let argCount = args.length;
16505+
if (argCount <= max && hasSpreadArgument) {
16506+
argCount--;
16507+
}
1650516508
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_a_minimum_of_1 :
1650616509
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
1650716510
hasSpreadArgument ? Diagnostics.Expected_0_arguments_but_got_a_minimum_of_1 :

0 commit comments

Comments
 (0)