Skip to content

Commit 5d332ae

Browse files
committed
add diagnostic message for call signature length mismatch
1 parent 6a3b499 commit 5d332ae

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18480,6 +18480,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1848018480
const sourceHasMoreParameters = !hasEffectiveRestParameter(target) &&
1848118481
(checkMode & SignatureCheckMode.StrictArity ? hasEffectiveRestParameter(source) || getParameterCount(source) > targetCount : getMinArgumentCount(source) > targetCount);
1848218482
if (sourceHasMoreParameters) {
18483+
if (reportErrors) {
18484+
errorReporter!(Diagnostics.Call_signature_0_expects_more_arguments_than_call_signature_1, signatureToString(source), signatureToString(target));
18485+
}
1848318486
return Ternary.False;
1848418487
}
1848518488

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,6 +3563,10 @@
35633563
"category": "Error",
35643564
"code": 2845
35653565
},
3566+
"Call signature '{0}' expects more arguments than call signature '{1}'.": {
3567+
"category": "Error",
3568+
"code": 2846
3569+
},
35663570

35673571
"Import declaration '{0}' is using private name '{1}'.": {
35683572
"category": "Error",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/signatureLengthMismatchCall.ts(5,15): error TS2345: Argument of type '(a: number, b: number) => void' is not assignable to parameter of type '(a: number) => void'.
2+
Call signature '(a: number, b: number): void' expects more arguments than call signature '(a: number): void'.
3+
4+
5+
==== tests/cases/compiler/signatureLengthMismatchCall.ts (1 errors) ====
6+
function takesCallback(fn: (a: number) => void) {
7+
// ...
8+
}
9+
10+
takesCallback((a: number, b: number) => {});
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
!!! error TS2345: Argument of type '(a: number, b: number) => void' is not assignable to parameter of type '(a: number) => void'.
13+
!!! error TS2345: Call signature '(a: number, b: number): void' expects more arguments than call signature '(a: number): void'.
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [signatureLengthMismatchCall.ts]
2+
function takesCallback(fn: (a: number) => void) {
3+
// ...
4+
}
5+
6+
takesCallback((a: number, b: number) => {});
7+
8+
9+
//// [signatureLengthMismatchCall.js]
10+
function takesCallback(fn) {
11+
// ...
12+
}
13+
takesCallback(function (a, b) { });
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/signatureLengthMismatchCall.ts ===
2+
function takesCallback(fn: (a: number) => void) {
3+
>takesCallback : Symbol(takesCallback, Decl(signatureLengthMismatchCall.ts, 0, 0))
4+
>fn : Symbol(fn, Decl(signatureLengthMismatchCall.ts, 0, 23))
5+
>a : Symbol(a, Decl(signatureLengthMismatchCall.ts, 0, 28))
6+
7+
// ...
8+
}
9+
10+
takesCallback((a: number, b: number) => {});
11+
>takesCallback : Symbol(takesCallback, Decl(signatureLengthMismatchCall.ts, 0, 0))
12+
>a : Symbol(a, Decl(signatureLengthMismatchCall.ts, 4, 15))
13+
>b : Symbol(b, Decl(signatureLengthMismatchCall.ts, 4, 25))
14+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/signatureLengthMismatchCall.ts ===
2+
function takesCallback(fn: (a: number) => void) {
3+
>takesCallback : (fn: (a: number) => void) => void
4+
>fn : (a: number) => void
5+
>a : number
6+
7+
// ...
8+
}
9+
10+
takesCallback((a: number, b: number) => {});
11+
>takesCallback((a: number, b: number) => {}) : void
12+
>takesCallback : (fn: (a: number) => void) => void
13+
>(a: number, b: number) => {} : (a: number, b: number) => void
14+
>a : number
15+
>b : number
16+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function takesCallback(fn: (a: number) => void) {
2+
// ...
3+
}
4+
5+
takesCallback((a: number, b: number) => {});

0 commit comments

Comments
 (0)