From a3ead92046141ea1be1aa975ceea297d7ea5af5d Mon Sep 17 00:00:00 2001 From: Masato Urai Date: Tue, 26 Jan 2021 19:54:36 +0900 Subject: [PATCH] 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 --- src/compiler/checker.ts | 7 ++++--- .../functionParameterArityMismatch.errors.txt | 6 +++++- .../reference/functionParameterArityMismatch.js | 7 +++++++ .../reference/functionParameterArityMismatch.symbols | 3 +++ .../reference/functionParameterArityMismatch.types | 12 ++++++++++++ .../cases/compiler/functionParameterArityMismatch.ts | 1 + 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3de21bebf76..3756e5076bf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); diff --git a/tests/baselines/reference/functionParameterArityMismatch.errors.txt b/tests/baselines/reference/functionParameterArityMismatch.errors.txt index aa870c23028..1182ecb58ab 100644 --- a/tests/baselines/reference/functionParameterArityMismatch.errors.txt +++ b/tests/baselines/reference/functionParameterArityMismatch.errors.txt @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/functionParameterArityMismatch.js b/tests/baselines/reference/functionParameterArityMismatch.js index cfbe27d5f29..ec8809a517f 100644 --- a/tests/baselines/reference/functionParameterArityMismatch.js +++ b/tests/baselines/reference/functionParameterArityMismatch.js @@ -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])); diff --git a/tests/baselines/reference/functionParameterArityMismatch.symbols b/tests/baselines/reference/functionParameterArityMismatch.symbols index 5615ac694ef..638b7c8c58d 100644 --- a/tests/baselines/reference/functionParameterArityMismatch.symbols +++ b/tests/baselines/reference/functionParameterArityMismatch.symbols @@ -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)) + diff --git a/tests/baselines/reference/functionParameterArityMismatch.types b/tests/baselines/reference/functionParameterArityMismatch.types index f7117a2eef3..9de4e8d2601 100644 --- a/tests/baselines/reference/functionParameterArityMismatch.types +++ b/tests/baselines/reference/functionParameterArityMismatch.types @@ -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 + diff --git a/tests/cases/compiler/functionParameterArityMismatch.ts b/tests/cases/compiler/functionParameterArityMismatch.ts index 738568012d2..7231dcc3d37 100644 --- a/tests/cases/compiler/functionParameterArityMismatch.ts +++ b/tests/cases/compiler/functionParameterArityMismatch.ts @@ -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);