Skip to content

Commit ada39c5

Browse files
authored
Merge pull request #15861 from Microsoft/improve-arity-error
Improve arity error messages
2 parents 9935751 + 42d8968 commit ada39c5

File tree

91 files changed

+521
-494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+521
-494
lines changed

src/compiler/checker.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15336,7 +15336,7 @@ namespace ts {
1533615336
}
1533715337
}
1533815338

15339-
function resolveCall(node: CallLikeExpression, signatures: Signature[], candidatesOutArray: Signature[], headMessage?: DiagnosticMessage): Signature {
15339+
function resolveCall(node: CallLikeExpression, signatures: Signature[], candidatesOutArray: Signature[], fallbackError?: DiagnosticMessage): Signature {
1534015340
const isTaggedTemplate = node.kind === SyntaxKind.TaggedTemplateExpression;
1534115341
const isDecorator = node.kind === SyntaxKind.Decorator;
1534215342
const isJsxOpeningOrSelfClosingElement = isJsxOpeningLikeElement(node);
@@ -15371,7 +15371,7 @@ namespace ts {
1537115371
// reorderCandidates fills up the candidates array directly
1537215372
reorderCandidates(signatures, candidates);
1537315373
if (!candidates.length) {
15374-
reportError(Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target);
15374+
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Call_target_does_not_contain_any_signatures));
1537515375
return resolveErrorCall(node);
1537615376
}
1537715377

@@ -15479,7 +15479,7 @@ namespace ts {
1547915479
else if (candidateForTypeArgumentError) {
1548015480
if (!isTaggedTemplate && !isDecorator && typeArguments) {
1548115481
const typeArguments = (<CallExpression>node).typeArguments;
15482-
checkTypeArguments(candidateForTypeArgumentError, typeArguments, map(typeArguments, getTypeFromTypeNode), /*reportErrors*/ true, headMessage);
15482+
checkTypeArguments(candidateForTypeArgumentError, typeArguments, map(typeArguments, getTypeFromTypeNode), /*reportErrors*/ true, fallbackError);
1548315483
}
1548415484
else {
1548515485
Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0);
@@ -15490,15 +15490,44 @@ namespace ts {
1549015490
Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly,
1549115491
typeToString(failedTypeParameter));
1549215492

15493-
if (headMessage) {
15494-
diagnosticChainHead = chainDiagnosticMessages(diagnosticChainHead, headMessage);
15493+
if (fallbackError) {
15494+
diagnosticChainHead = chainDiagnosticMessages(diagnosticChainHead, fallbackError);
1549515495
}
1549615496

1549715497
reportNoCommonSupertypeError(inferenceCandidates, (<JsxOpeningLikeElement>node).tagName || (<CallExpression>node).expression || (<TaggedTemplateExpression>node).tag, diagnosticChainHead);
1549815498
}
1549915499
}
15500-
else {
15501-
reportError(Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target);
15500+
else if (typeArguments && every(signatures, sig => length(sig.typeParameters) !== typeArguments.length)) {
15501+
let min = Number.POSITIVE_INFINITY;
15502+
let max = Number.NEGATIVE_INFINITY;
15503+
for (const sig of signatures) {
15504+
min = Math.min(min, getMinTypeArgumentCount(sig.typeParameters));
15505+
max = Math.max(max, length(sig.typeParameters));
15506+
}
15507+
const paramCount = min < max ? `${min}-${max}` : min;
15508+
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_type_arguments_but_got_1, paramCount, typeArguments.length));
15509+
}
15510+
else if (args) {
15511+
let min = Number.POSITIVE_INFINITY;
15512+
let max = Number.NEGATIVE_INFINITY;
15513+
for (const sig of signatures) {
15514+
min = Math.min(min, sig.minArgumentCount);
15515+
max = Math.max(max, sig.parameters.length);
15516+
}
15517+
const hasRestParameter = some(signatures, sig => sig.hasRestParameter);
15518+
const hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
15519+
const paramCount = hasRestParameter ? min :
15520+
min < max ? `${min}-${max}` :
15521+
min;
15522+
const argCount = args.length - (hasSpreadArgument ? 1 : 0);
15523+
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_a_minimum_of_1 :
15524+
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
15525+
hasSpreadArgument ? Diagnostics.Expected_0_arguments_but_got_a_minimum_of_1 :
15526+
Diagnostics.Expected_0_arguments_but_got_1;
15527+
diagnostics.add(createDiagnosticForNode(node, error, paramCount, argCount));
15528+
}
15529+
else if (fallbackError) {
15530+
diagnostics.add(createDiagnosticForNode(node, fallbackError));
1550215531
}
1550315532

1550415533
// No signature was applicable. We have already reported the errors for the invalid signature.
@@ -15519,16 +15548,6 @@ namespace ts {
1551915548

1552015549
return resolveErrorCall(node);
1552115550

15522-
function reportError(message: DiagnosticMessage, arg0?: string, arg1?: string, arg2?: string): void {
15523-
let errorInfo: DiagnosticMessageChain;
15524-
errorInfo = chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2);
15525-
if (headMessage) {
15526-
errorInfo = chainDiagnosticMessages(errorInfo, headMessage);
15527-
}
15528-
15529-
diagnostics.add(createDiagnosticForNodeFromMessageChain(node, errorInfo));
15530-
}
15531-
1553215551
function chooseOverload(candidates: Signature[], relation: Map<RelationComparisonResult>, signatureHelpTrailingComma = false) {
1553315552
for (const originalCandidate of candidates) {
1553415553
if (!hasCorrectArity(node, args, originalCandidate, signatureHelpTrailingComma)) {

src/compiler/diagnosticMessages.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@
10671067
"category": "Error",
10681068
"code": 2345
10691069
},
1070-
"Supplied parameters do not match any signature of call target.": {
1070+
"Call target does not contain any signatures.": {
10711071
"category": "Error",
10721072
"code": 2346
10731073
},
@@ -1859,6 +1859,26 @@
18591859
"category": "Error",
18601860
"code": 2553
18611861
},
1862+
"Expected {0} arguments, but got {1}.": {
1863+
"category": "Error",
1864+
"code": 2554
1865+
},
1866+
"Expected at least {0} arguments, but got {1}.": {
1867+
"category": "Error",
1868+
"code": 2555
1869+
},
1870+
"Expected {0} arguments, but got a minimum of {1}.": {
1871+
"category": "Error",
1872+
"code": 2556
1873+
},
1874+
"Expected at least {0} arguments, but got a minimum of {1}.": {
1875+
"category": "Error",
1876+
"code": 2557
1877+
},
1878+
"Expected {0} type arguments, but got {1}.": {
1879+
"category": "Error",
1880+
"code": 2558
1881+
},
18621882
"JSX element attributes type '{0}' may not be a union type.": {
18631883
"category": "Error",
18641884
"code": 2600

tests/baselines/reference/ES5SymbolProperty5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/Symbols/ES5SymbolProperty5.ts(7,1): error TS2346: Supplied parameters do not match any signature of call target.
1+
tests/cases/conformance/Symbols/ES5SymbolProperty5.ts(7,1): error TS2554: Expected 0 arguments, but got 1.
22

33

44
==== tests/cases/conformance/Symbols/ES5SymbolProperty5.ts (1 errors) ====
@@ -10,4 +10,4 @@ tests/cases/conformance/Symbols/ES5SymbolProperty5.ts(7,1): error TS2346: Suppli
1010

1111
(new C)[Symbol.iterator](0) // Should error
1212
~~~~~~~~~~~~~~~~~~~~~~~~~~~
13-
!!! error TS2346: Supplied parameters do not match any signature of call target.
13+
!!! error TS2554: Expected 0 arguments, but got 1.

tests/baselines/reference/assigningFromObjectToAnythingElse.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(3,1): error TS2322: Type 'Object' is not assignable to type 'RegExp'.
22
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
33
Property 'exec' is missing in type 'Object'.
4-
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(5,17): error TS2346: Supplied parameters do not match any signature of call target.
5-
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(6,17): error TS2346: Supplied parameters do not match any signature of call target.
4+
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(5,17): error TS2558: Expected 0 type arguments, but got 1.
5+
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(6,17): error TS2558: Expected 0 type arguments, but got 1.
66
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(8,5): error TS2322: Type 'Object' is not assignable to type 'Error'.
77
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
88
Property 'name' is missing in type 'Object'.
@@ -19,10 +19,10 @@ tests/cases/compiler/assigningFromObjectToAnythingElse.ts(8,5): error TS2322: Ty
1919

2020
var a: String = Object.create<Object>("");
2121
~~~~~~~~~~~~~~~~~~~~~~~~~
22-
!!! error TS2346: Supplied parameters do not match any signature of call target.
22+
!!! error TS2558: Expected 0 type arguments, but got 1.
2323
var c: String = Object.create<Number>(1);
2424
~~~~~~~~~~~~~~~~~~~~~~~~
25-
!!! error TS2346: Supplied parameters do not match any signature of call target.
25+
!!! error TS2558: Expected 0 type arguments, but got 1.
2626

2727
var w: Error = new Object();
2828
~

tests/baselines/reference/baseCheck.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/baseCheck.ts(9,18): error TS2552: Cannot find name 'loc'. Did you mean 'ELoc'?
2-
tests/cases/compiler/baseCheck.ts(17,53): error TS2346: Supplied parameters do not match any signature of call target.
2+
tests/cases/compiler/baseCheck.ts(17,53): error TS2554: Expected 2 arguments, but got 1.
33
tests/cases/compiler/baseCheck.ts(17,59): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
44
tests/cases/compiler/baseCheck.ts(18,62): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
55
tests/cases/compiler/baseCheck.ts(19,59): error TS2345: Argument of type '"hello"' is not assignable to parameter of type 'number'.
@@ -30,7 +30,7 @@ tests/cases/compiler/baseCheck.ts(26,9): error TS2304: Cannot find name 'x'.
3030

3131
class D extends C { constructor(public z: number) { super(this.z) } } // too few params
3232
~~~~~~~~~~~~~
33-
!!! error TS2346: Supplied parameters do not match any signature of call target.
33+
!!! error TS2554: Expected 2 arguments, but got 1.
3434
~~~~
3535
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
3636
class E extends C { constructor(public z: number) { super(0, this.z) } }
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(3,18): error TS2393: Duplicate function implementation.
2-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(5,9): error TS2346: Supplied parameters do not match any signature of call target.
2+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(5,9): error TS2554: Expected 0 arguments, but got 1.
33
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(8,18): error TS2393: Duplicate function implementation.
4-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(10,9): error TS2346: Supplied parameters do not match any signature of call target.
5-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(12,5): error TS2346: Supplied parameters do not match any signature of call target.
6-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(16,1): error TS2346: Supplied parameters do not match any signature of call target.
4+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(10,9): error TS2554: Expected 0 arguments, but got 1.
5+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(12,5): error TS2554: Expected 0 arguments, but got 1.
6+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(16,1): error TS2554: Expected 1 arguments, but got 0.
77

88

99
==== tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts (6 errors) ====
@@ -15,7 +15,7 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(16,1): error T
1515
foo();
1616
foo(10); // not ok
1717
~~~~~~~
18-
!!! error TS2346: Supplied parameters do not match any signature of call target.
18+
!!! error TS2554: Expected 0 arguments, but got 1.
1919
}
2020
else {
2121
function foo() { } // duplicate function
@@ -24,14 +24,14 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(16,1): error T
2424
foo();
2525
foo(10); // not ok
2626
~~~~~~~
27-
!!! error TS2346: Supplied parameters do not match any signature of call target.
27+
!!! error TS2554: Expected 0 arguments, but got 1.
2828
}
2929
foo(10); // not ok
3030
~~~~~~~
31-
!!! error TS2346: Supplied parameters do not match any signature of call target.
31+
!!! error TS2554: Expected 0 arguments, but got 1.
3232
foo();
3333
}
3434
foo(10);
3535
foo(); // not ok - needs number
3636
~~~~~
37-
!!! error TS2346: Supplied parameters do not match any signature of call target.
37+
!!! error TS2554: Expected 1 arguments, but got 0.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(3,18): error TS2393: Duplicate function implementation.
2-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(5,9): error TS2346: Supplied parameters do not match any signature of call target.
2+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(5,9): error TS2554: Expected 0 arguments, but got 1.
33
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(8,18): error TS2393: Duplicate function implementation.
4-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(10,9): error TS2346: Supplied parameters do not match any signature of call target.
5-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(12,5): error TS2346: Supplied parameters do not match any signature of call target.
6-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(16,1): error TS2346: Supplied parameters do not match any signature of call target.
4+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(10,9): error TS2554: Expected 0 arguments, but got 1.
5+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(12,5): error TS2554: Expected 0 arguments, but got 1.
6+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(16,1): error TS2554: Expected 1 arguments, but got 0.
77

88

99
==== tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts (6 errors) ====
@@ -15,7 +15,7 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(16,1): error T
1515
foo();
1616
foo(10); // not ok
1717
~~~~~~~
18-
!!! error TS2346: Supplied parameters do not match any signature of call target.
18+
!!! error TS2554: Expected 0 arguments, but got 1.
1919
}
2020
else {
2121
function foo() { } // duplicate
@@ -24,14 +24,14 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(16,1): error T
2424
foo();
2525
foo(10);// not ok
2626
~~~~~~~
27-
!!! error TS2346: Supplied parameters do not match any signature of call target.
27+
!!! error TS2554: Expected 0 arguments, but got 1.
2828
}
2929
foo(10); // not ok
3030
~~~~~~~
31-
!!! error TS2346: Supplied parameters do not match any signature of call target.
31+
!!! error TS2554: Expected 0 arguments, but got 1.
3232
foo();
3333
}
3434
foo(10);
3535
foo(); // not ok - needs number
3636
~~~~~
37-
!!! error TS2346: Supplied parameters do not match any signature of call target.
37+
!!! error TS2554: Expected 1 arguments, but got 0.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(4,18): error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES3' or 'ES5'.
2-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(6,9): error TS2346: Supplied parameters do not match any signature of call target.
2+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(6,9): error TS2554: Expected 0 arguments, but got 1.
33
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(9,18): error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES3' or 'ES5'.
4-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(11,9): error TS2346: Supplied parameters do not match any signature of call target.
5-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(14,5): error TS2346: Supplied parameters do not match any signature of call target.
6-
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(17,1): error TS2346: Supplied parameters do not match any signature of call target.
4+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(11,9): error TS2554: Expected 0 arguments, but got 1.
5+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(14,5): error TS2554: Expected 1 arguments, but got 0.
6+
tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(17,1): error TS2554: Expected 1 arguments, but got 0.
77

88

99
==== tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts (6 errors) ====
@@ -16,7 +16,7 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(17,1): e
1616
foo();
1717
foo(10); // not ok
1818
~~~~~~~
19-
!!! error TS2346: Supplied parameters do not match any signature of call target.
19+
!!! error TS2554: Expected 0 arguments, but got 1.
2020
}
2121
else {
2222
function foo() { } // Error to declare function in block scope
@@ -25,14 +25,14 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(17,1): e
2525
foo();
2626
foo(10); // not ok
2727
~~~~~~~
28-
!!! error TS2346: Supplied parameters do not match any signature of call target.
28+
!!! error TS2554: Expected 0 arguments, but got 1.
2929
}
3030
foo(10);
3131
foo(); // not ok - needs number
3232
~~~~~
33-
!!! error TS2346: Supplied parameters do not match any signature of call target.
33+
!!! error TS2554: Expected 1 arguments, but got 0.
3434
}
3535
foo(10);
3636
foo(); // not ok - needs number
3737
~~~~~
38-
!!! error TS2346: Supplied parameters do not match any signature of call target.
38+
!!! error TS2554: Expected 1 arguments, but got 0.

0 commit comments

Comments
 (0)