From 93b1eafb9f9d6bd6c82fe99f2ed752d9772285a4 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 15 May 2017 15:25:21 -0700 Subject: [PATCH] Improve arity error messages For calls with one signature. --- src/compiler/checker.ts | 22 +++++++++++++++++++++- src/compiler/diagnosticMessages.json | 8 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d6f56a69b9a..5be1fb633cd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15370,7 +15370,27 @@ namespace ts { } } else { - reportError(Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); + if (signatures.length === 1) { + if (typeArguments && typeArguments.length !== length(signatures[0].typeParameters)) { + const minTypeArgumentCount = getMinTypeArgumentCount(signatures[0].typeParameters); + const paramMessage = length(signatures[0].typeParameters) > minTypeArgumentCount ? + `${minTypeArgumentCount}-${length(signatures[0].typeParameters)}` : + minTypeArgumentCount.toString(); + reportError(Diagnostics.Expected_0_type_arguments_but_got_1, paramMessage, typeArguments.length.toString()); + } + else { + const argMessage = getSpreadArgumentIndex(args) > -1 ? + "a minimum of " + (args.length - 1) : + args.length.toString(); + const paramMessage = signatures[0].hasRestParameter ? "at least " + signatures[0].minArgumentCount : + signatures[0].minArgumentCount < signatures[0].parameters.length ? `${signatures[0].minArgumentCount}-${signatures[0].parameters.length}` : + signatures[0].minArgumentCount.toString(); + reportError(Diagnostics.Expected_0_arguments_but_got_1, paramMessage, argMessage); + } + } + else { + reportError(Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); + } } // No signature was applicable. We have already reported the errors for the invalid signature. diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4aee7a37a88..849b780380f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1855,6 +1855,14 @@ "category": "Error", "code": 2552 }, + "Expected {0} arguments, but got {1}.": { + "category": "Error", + "code": 2553 + }, + "Expected {0} type arguments, but got {1}.": { + "category": "Error", + "code": 2554 + }, "JSX element attributes type '{0}' may not be a union type.": { "category": "Error", "code": 2600