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
This commit is contained in:
Masato Urai 2021-01-26 19:54:36 +09:00 committed by Eli Barzilay
parent 4fc9c8446d
commit a3ead92046
6 changed files with 32 additions and 4 deletions

View File

@ -27709,6 +27709,10 @@ namespace ts {
max = Math.max(max, maxCount);
}
if (min < argCount && argCount < max) {
return getDiagnosticForCallNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
}
const hasRestParameter = some(signatures, hasEffectiveRestParameter);
const paramRange = hasRestParameter ? min :
min < max ? min + "-" + max :
@ -27742,9 +27746,6 @@ namespace ts {
);
}
}
if (min < argCount && argCount < max) {
return getDiagnosticForCallNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
}
if (!hasSpreadArgument && argCount < min) {
const diagnostic = getDiagnosticForCallNode(node, error, paramRange, argCount);

View File

@ -5,9 +5,10 @@ tests/cases/compiler/functionParameterArityMismatch.ts(11,1): error TS2575: No o
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.
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.
tests/cases/compiler/functionParameterArityMismatch.ts(14,22): error TS2554: Expected 0-6 arguments, but got 7.
tests/cases/compiler/functionParameterArityMismatch.ts(15,12): error TS2556: Expected 0-6 arguments, but got 5 or more.
==== tests/cases/compiler/functionParameterArityMismatch.ts (7 errors) ====
==== tests/cases/compiler/functionParameterArityMismatch.ts (8 errors) ====
declare function f1(a: number);
declare function f1(a: number, b: number, c: number);
f1();
@ -37,4 +38,7 @@ tests/cases/compiler/functionParameterArityMismatch.ts(14,22): error TS2554: Exp
f2(1, 2, 3, 4, 5, 6, 7);
~
!!! error TS2554: Expected 0-6 arguments, but got 7.
f2(...[1], 2, 3, 4, 5, 6);
~~~~~~~~~~~~~
!!! error TS2556: Expected 0-6 arguments, but got 5 or more.

View File

@ -13,9 +13,15 @@ f2(1);
f2(1, 2, 3);
f2(1, 2, 3, 4, 5);
f2(1, 2, 3, 4, 5, 6, 7);
f2(...[1], 2, 3, 4, 5, 6);
//// [functionParameterArityMismatch.js]
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
f1();
f1(1, 2);
f1(1, 2, 3, 4);
@ -23,3 +29,4 @@ f2(1);
f2(1, 2, 3);
f2(1, 2, 3, 4, 5);
f2(1, 2, 3, 4, 5, 6, 7);
f2.apply(void 0, __spreadArray(__spreadArray([], [1]), [2, 3, 4, 5, 6]));

View File

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

View File

@ -83,3 +83,15 @@ f2(1, 2, 3, 4, 5, 6, 7);
>6 : 6
>7 : 7
f2(...[1], 2, 3, 4, 5, 6);
>f2(...[1], 2, 3, 4, 5, 6) : any
>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; }
>...[1] : number
>[1] : number[]
>1 : 1
>2 : 2
>3 : 3
>4 : 4
>5 : 5
>6 : 6

View File

@ -12,3 +12,4 @@ f2(1);
f2(1, 2, 3);
f2(1, 2, 3, 4, 5);
f2(1, 2, 3, 4, 5, 6, 7);
f2(...[1], 2, 3, 4, 5, 6);