Skip to content

Commit a3ead92

Browse files
urawayelibarzilay
authored andcommitted
Improve error message for overload that takes spread arguments
The original error message on the last line I have added to in functionParameterArityMismatch.ts was No overload expects 5 arguments, but overloads do exist that expect either 4 or Infinity arguments. even if we do not define a function that takes Infinity arguments. This PR changes it to this: Expected 0-6 arguments, but got 5 or more. I feel it is still a bit strange but much more understandable. Fixes #42418
1 parent 4fc9c84 commit a3ead92

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27709,6 +27709,10 @@ namespace ts {
2770927709
max = Math.max(max, maxCount);
2771027710
}
2771127711

27712+
if (min < argCount && argCount < max) {
27713+
return getDiagnosticForCallNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
27714+
}
27715+
2771227716
const hasRestParameter = some(signatures, hasEffectiveRestParameter);
2771327717
const paramRange = hasRestParameter ? min :
2771427718
min < max ? min + "-" + max :
@@ -27742,9 +27746,6 @@ namespace ts {
2774227746
);
2774327747
}
2774427748
}
27745-
if (min < argCount && argCount < max) {
27746-
return getDiagnosticForCallNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
27747-
}
2774827749

2774927750
if (!hasSpreadArgument && argCount < min) {
2775027751
const diagnostic = getDiagnosticForCallNode(node, error, paramRange, argCount);

tests/baselines/reference/functionParameterArityMismatch.errors.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ tests/cases/compiler/functionParameterArityMismatch.ts(11,1): error TS2575: No o
55
tests/cases/compiler/functionParameterArityMismatch.ts(12,1): error TS2575: No overload expects 3 arguments, but overloads do exist that expect either 2 or 4 arguments.
66
tests/cases/compiler/functionParameterArityMismatch.ts(13,1): error TS2575: No overload expects 5 arguments, but overloads do exist that expect either 4 or 6 arguments.
77
tests/cases/compiler/functionParameterArityMismatch.ts(14,22): error TS2554: Expected 0-6 arguments, but got 7.
8+
tests/cases/compiler/functionParameterArityMismatch.ts(15,12): error TS2556: Expected 0-6 arguments, but got 5 or more.
89

910

10-
==== tests/cases/compiler/functionParameterArityMismatch.ts (7 errors) ====
11+
==== tests/cases/compiler/functionParameterArityMismatch.ts (8 errors) ====
1112
declare function f1(a: number);
1213
declare function f1(a: number, b: number, c: number);
1314
f1();
@@ -37,4 +38,7 @@ tests/cases/compiler/functionParameterArityMismatch.ts(14,22): error TS2554: Exp
3738
f2(1, 2, 3, 4, 5, 6, 7);
3839
~
3940
!!! error TS2554: Expected 0-6 arguments, but got 7.
41+
f2(...[1], 2, 3, 4, 5, 6);
42+
~~~~~~~~~~~~~
43+
!!! error TS2556: Expected 0-6 arguments, but got 5 or more.
4044

tests/baselines/reference/functionParameterArityMismatch.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ f2(1);
1313
f2(1, 2, 3);
1414
f2(1, 2, 3, 4, 5);
1515
f2(1, 2, 3, 4, 5, 6, 7);
16+
f2(...[1], 2, 3, 4, 5, 6);
1617

1718

1819
//// [functionParameterArityMismatch.js]
20+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
21+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
22+
to[j] = from[i];
23+
return to;
24+
};
1925
f1();
2026
f1(1, 2);
2127
f1(1, 2, 3, 4);
2228
f2(1);
2329
f2(1, 2, 3);
2430
f2(1, 2, 3, 4, 5);
2531
f2(1, 2, 3, 4, 5, 6, 7);
32+
f2.apply(void 0, __spreadArray(__spreadArray([], [1]), [2, 3, 4, 5, 6]));

tests/baselines/reference/functionParameterArityMismatch.symbols

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ f2(1, 2, 3, 4, 5);
5454
f2(1, 2, 3, 4, 5, 6, 7);
5555
>f2 : Symbol(f2, Decl(functionParameterArityMismatch.ts, 4, 15), Decl(functionParameterArityMismatch.ts, 6, 22), Decl(functionParameterArityMismatch.ts, 7, 42), Decl(functionParameterArityMismatch.ts, 8, 64))
5656

57+
f2(...[1], 2, 3, 4, 5, 6);
58+
>f2 : Symbol(f2, Decl(functionParameterArityMismatch.ts, 4, 15), Decl(functionParameterArityMismatch.ts, 6, 22), Decl(functionParameterArityMismatch.ts, 7, 42), Decl(functionParameterArityMismatch.ts, 8, 64))
59+

tests/baselines/reference/functionParameterArityMismatch.types

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ f2(1, 2, 3, 4, 5, 6, 7);
8383
>6 : 6
8484
>7 : 7
8585

86+
f2(...[1], 2, 3, 4, 5, 6);
87+
>f2(...[1], 2, 3, 4, 5, 6) : any
88+
>f2 : { (): any; (a: number, b: number): any; (a: number, b: number, c: number, d: number): any; (a: number, b: number, c: number, d: number, e: number, f: number): any; }
89+
>...[1] : number
90+
>[1] : number[]
91+
>1 : 1
92+
>2 : 2
93+
>3 : 3
94+
>4 : 4
95+
>5 : 5
96+
>6 : 6
97+

tests/cases/compiler/functionParameterArityMismatch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ f2(1);
1212
f2(1, 2, 3);
1313
f2(1, 2, 3, 4, 5);
1414
f2(1, 2, 3, 4, 5, 6, 7);
15+
f2(...[1], 2, 3, 4, 5, 6);

0 commit comments

Comments
 (0)