mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Fix error message when type argument arity is wrong (#28366)
* Fix error message when type argument arity is wrong * Parenthesize 's' in plurals * Update baseline * Update baseline * Use old error message * Revert parantheses
This commit is contained in:
committed by
Nathan Shively-Sanders
parent
3c408d8054
commit
e6aa992095
+24
-7
@@ -19890,14 +19890,31 @@ namespace ts {
|
||||
}
|
||||
|
||||
function getTypeArgumentArityError(node: Node, signatures: ReadonlyArray<Signature>, typeArguments: NodeArray<TypeNode>) {
|
||||
let min = Infinity;
|
||||
let max = -Infinity;
|
||||
for (const sig of signatures) {
|
||||
min = Math.min(min, getMinTypeArgumentCount(sig.typeParameters));
|
||||
max = Math.max(max, length(sig.typeParameters));
|
||||
const argCount = typeArguments.length;
|
||||
// No overloads exist
|
||||
if (signatures.length === 1) {
|
||||
const sig = signatures[0];
|
||||
const min = getMinTypeArgumentCount(sig.typeParameters);
|
||||
const max = length(sig.typeParameters);
|
||||
return createDiagnosticForNodeArray(getSourceFileOfNode(node), typeArguments, Diagnostics.Expected_0_type_arguments_but_got_1, min < max ? min + "-" + max : min , argCount);
|
||||
}
|
||||
const paramCount = min === max ? min : min + "-" + max;
|
||||
return createDiagnosticForNodeArray(getSourceFileOfNode(node), typeArguments, Diagnostics.Expected_0_type_arguments_but_got_1, paramCount, typeArguments.length);
|
||||
// Overloads exist
|
||||
let belowArgCount = -Infinity;
|
||||
let aboveArgCount = Infinity;
|
||||
for (const sig of signatures) {
|
||||
const min = getMinTypeArgumentCount(sig.typeParameters);
|
||||
const max = length(sig.typeParameters);
|
||||
if (min > argCount) {
|
||||
aboveArgCount = Math.min(aboveArgCount, min);
|
||||
}
|
||||
else if (max < argCount) {
|
||||
belowArgCount = Math.max(belowArgCount, max);
|
||||
}
|
||||
}
|
||||
if (belowArgCount !== -Infinity && aboveArgCount !== Infinity) {
|
||||
return createDiagnosticForNodeArray(getSourceFileOfNode(node), typeArguments, Diagnostics.No_overload_expects_0_type_arguments_but_overloads_do_exist_that_expect_either_1_or_2_type_arguments, argCount, belowArgCount, aboveArgCount);
|
||||
}
|
||||
return createDiagnosticForNodeArray(getSourceFileOfNode(node), typeArguments, Diagnostics.Expected_0_type_arguments_but_got_1, belowArgCount === -Infinity ? aboveArgCount : belowArgCount, argCount);
|
||||
}
|
||||
|
||||
function resolveCall(node: CallLikeExpression, signatures: ReadonlyArray<Signature>, candidatesOutArray: Signature[] | undefined, isForSignatureHelp: boolean, fallbackError?: DiagnosticMessage): Signature {
|
||||
|
||||
@@ -2533,6 +2533,10 @@
|
||||
"category": "Error",
|
||||
"code": 2742
|
||||
},
|
||||
"No overload expects {0} type arguments, but overloads do exist that expect either {1} or {2} type arguments.": {
|
||||
"category": "Error",
|
||||
"code": 2743
|
||||
},
|
||||
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(4,4): error TS2743: No overload expects 2 type arguments, but overloads do exist that expect either 1 or 3 type arguments.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(5,4): error TS2558: Expected 4 type arguments, but got 5.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(10,4): error TS2743: No overload expects 2 type arguments, but overloads do exist that expect either 1 or 3 type arguments.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(11,4): error TS2558: Expected 3 type arguments, but got 4.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(16,4): error TS2558: Expected 0 type arguments, but got 1.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(20,4): error TS2558: Expected 2-3 type arguments, but got 1.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(21,4): error TS2558: Expected 2-3 type arguments, but got 4.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(25,4): error TS2558: Expected 2 type arguments, but got 1.
|
||||
tests/cases/compiler/functionTypeArgumentArityErrors.ts(26,4): error TS2558: Expected 2 type arguments, but got 3.
|
||||
|
||||
|
||||
==== tests/cases/compiler/functionTypeArgumentArityErrors.ts (9 errors) ====
|
||||
// Overloaded functions with default type arguments
|
||||
declare function f1<A = any>(): void;
|
||||
declare function f1<A, B, C, D = any>(): void;
|
||||
f1<number, number>();
|
||||
~~~~~~~~~~~~~~
|
||||
!!! error TS2743: No overload expects 2 type arguments, but overloads do exist that expect either 1 or 3 type arguments.
|
||||
f1<number, number, number, number, number>();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 4 type arguments, but got 5.
|
||||
|
||||
// Overloaded functions with no default type arguments
|
||||
declare function f2<A>(): void;
|
||||
declare function f2<A, B, C>(): void;
|
||||
f2<number, number>();
|
||||
~~~~~~~~~~~~~~
|
||||
!!! error TS2743: No overload expects 2 type arguments, but overloads do exist that expect either 1 or 3 type arguments.
|
||||
f2<number, number, number, number>();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 3 type arguments, but got 4.
|
||||
|
||||
// Overloaded non-generic functions
|
||||
declare function f3(): void;
|
||||
declare function f3(a): void;
|
||||
f3<number>();
|
||||
~~~~~~
|
||||
!!! error TS2558: Expected 0 type arguments, but got 1.
|
||||
|
||||
// Generic function with default type parameters
|
||||
declare function f4<A, B, C = any>(): void;
|
||||
f4<number>();
|
||||
~~~~~~
|
||||
!!! error TS2558: Expected 2-3 type arguments, but got 1.
|
||||
f4<number, number, number, number>();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 2-3 type arguments, but got 4.
|
||||
|
||||
// Generic function with no default type arguments
|
||||
declare function f5<A, B>(): void;
|
||||
f5<number>();
|
||||
~~~~~~
|
||||
!!! error TS2558: Expected 2 type arguments, but got 1.
|
||||
f5<number, number, number>();
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 2 type arguments, but got 3.
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
//// [functionTypeArgumentArityErrors.ts]
|
||||
// Overloaded functions with default type arguments
|
||||
declare function f1<A = any>(): void;
|
||||
declare function f1<A, B, C, D = any>(): void;
|
||||
f1<number, number>();
|
||||
f1<number, number, number, number, number>();
|
||||
|
||||
// Overloaded functions with no default type arguments
|
||||
declare function f2<A>(): void;
|
||||
declare function f2<A, B, C>(): void;
|
||||
f2<number, number>();
|
||||
f2<number, number, number, number>();
|
||||
|
||||
// Overloaded non-generic functions
|
||||
declare function f3(): void;
|
||||
declare function f3(a): void;
|
||||
f3<number>();
|
||||
|
||||
// Generic function with default type parameters
|
||||
declare function f4<A, B, C = any>(): void;
|
||||
f4<number>();
|
||||
f4<number, number, number, number>();
|
||||
|
||||
// Generic function with no default type arguments
|
||||
declare function f5<A, B>(): void;
|
||||
f5<number>();
|
||||
f5<number, number, number>();
|
||||
|
||||
|
||||
//// [functionTypeArgumentArityErrors.js]
|
||||
f1();
|
||||
f1();
|
||||
f2();
|
||||
f2();
|
||||
f3();
|
||||
f4();
|
||||
f4();
|
||||
f5();
|
||||
f5();
|
||||
@@ -0,0 +1,72 @@
|
||||
=== tests/cases/compiler/functionTypeArgumentArityErrors.ts ===
|
||||
// Overloaded functions with default type arguments
|
||||
declare function f1<A = any>(): void;
|
||||
>f1 : Symbol(f1, Decl(functionTypeArgumentArityErrors.ts, 0, 0), Decl(functionTypeArgumentArityErrors.ts, 1, 37))
|
||||
>A : Symbol(A, Decl(functionTypeArgumentArityErrors.ts, 1, 20))
|
||||
|
||||
declare function f1<A, B, C, D = any>(): void;
|
||||
>f1 : Symbol(f1, Decl(functionTypeArgumentArityErrors.ts, 0, 0), Decl(functionTypeArgumentArityErrors.ts, 1, 37))
|
||||
>A : Symbol(A, Decl(functionTypeArgumentArityErrors.ts, 2, 20))
|
||||
>B : Symbol(B, Decl(functionTypeArgumentArityErrors.ts, 2, 22))
|
||||
>C : Symbol(C, Decl(functionTypeArgumentArityErrors.ts, 2, 25))
|
||||
>D : Symbol(D, Decl(functionTypeArgumentArityErrors.ts, 2, 28))
|
||||
|
||||
f1<number, number>();
|
||||
>f1 : Symbol(f1, Decl(functionTypeArgumentArityErrors.ts, 0, 0), Decl(functionTypeArgumentArityErrors.ts, 1, 37))
|
||||
|
||||
f1<number, number, number, number, number>();
|
||||
>f1 : Symbol(f1, Decl(functionTypeArgumentArityErrors.ts, 0, 0), Decl(functionTypeArgumentArityErrors.ts, 1, 37))
|
||||
|
||||
// Overloaded functions with no default type arguments
|
||||
declare function f2<A>(): void;
|
||||
>f2 : Symbol(f2, Decl(functionTypeArgumentArityErrors.ts, 4, 45), Decl(functionTypeArgumentArityErrors.ts, 7, 31))
|
||||
>A : Symbol(A, Decl(functionTypeArgumentArityErrors.ts, 7, 20))
|
||||
|
||||
declare function f2<A, B, C>(): void;
|
||||
>f2 : Symbol(f2, Decl(functionTypeArgumentArityErrors.ts, 4, 45), Decl(functionTypeArgumentArityErrors.ts, 7, 31))
|
||||
>A : Symbol(A, Decl(functionTypeArgumentArityErrors.ts, 8, 20))
|
||||
>B : Symbol(B, Decl(functionTypeArgumentArityErrors.ts, 8, 22))
|
||||
>C : Symbol(C, Decl(functionTypeArgumentArityErrors.ts, 8, 25))
|
||||
|
||||
f2<number, number>();
|
||||
>f2 : Symbol(f2, Decl(functionTypeArgumentArityErrors.ts, 4, 45), Decl(functionTypeArgumentArityErrors.ts, 7, 31))
|
||||
|
||||
f2<number, number, number, number>();
|
||||
>f2 : Symbol(f2, Decl(functionTypeArgumentArityErrors.ts, 4, 45), Decl(functionTypeArgumentArityErrors.ts, 7, 31))
|
||||
|
||||
// Overloaded non-generic functions
|
||||
declare function f3(): void;
|
||||
>f3 : Symbol(f3, Decl(functionTypeArgumentArityErrors.ts, 10, 37), Decl(functionTypeArgumentArityErrors.ts, 13, 28))
|
||||
|
||||
declare function f3(a): void;
|
||||
>f3 : Symbol(f3, Decl(functionTypeArgumentArityErrors.ts, 10, 37), Decl(functionTypeArgumentArityErrors.ts, 13, 28))
|
||||
>a : Symbol(a, Decl(functionTypeArgumentArityErrors.ts, 14, 20))
|
||||
|
||||
f3<number>();
|
||||
>f3 : Symbol(f3, Decl(functionTypeArgumentArityErrors.ts, 10, 37), Decl(functionTypeArgumentArityErrors.ts, 13, 28))
|
||||
|
||||
// Generic function with default type parameters
|
||||
declare function f4<A, B, C = any>(): void;
|
||||
>f4 : Symbol(f4, Decl(functionTypeArgumentArityErrors.ts, 15, 13))
|
||||
>A : Symbol(A, Decl(functionTypeArgumentArityErrors.ts, 18, 20))
|
||||
>B : Symbol(B, Decl(functionTypeArgumentArityErrors.ts, 18, 22))
|
||||
>C : Symbol(C, Decl(functionTypeArgumentArityErrors.ts, 18, 25))
|
||||
|
||||
f4<number>();
|
||||
>f4 : Symbol(f4, Decl(functionTypeArgumentArityErrors.ts, 15, 13))
|
||||
|
||||
f4<number, number, number, number>();
|
||||
>f4 : Symbol(f4, Decl(functionTypeArgumentArityErrors.ts, 15, 13))
|
||||
|
||||
// Generic function with no default type arguments
|
||||
declare function f5<A, B>(): void;
|
||||
>f5 : Symbol(f5, Decl(functionTypeArgumentArityErrors.ts, 20, 37))
|
||||
>A : Symbol(A, Decl(functionTypeArgumentArityErrors.ts, 23, 20))
|
||||
>B : Symbol(B, Decl(functionTypeArgumentArityErrors.ts, 23, 22))
|
||||
|
||||
f5<number>();
|
||||
>f5 : Symbol(f5, Decl(functionTypeArgumentArityErrors.ts, 20, 37))
|
||||
|
||||
f5<number, number, number>();
|
||||
>f5 : Symbol(f5, Decl(functionTypeArgumentArityErrors.ts, 20, 37))
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
=== tests/cases/compiler/functionTypeArgumentArityErrors.ts ===
|
||||
// Overloaded functions with default type arguments
|
||||
declare function f1<A = any>(): void;
|
||||
>f1 : { <A = any>(): void; <A, B, C, D = any>(): void; }
|
||||
|
||||
declare function f1<A, B, C, D = any>(): void;
|
||||
>f1 : { <A = any>(): void; <A, B, C, D = any>(): void; }
|
||||
|
||||
f1<number, number>();
|
||||
>f1<number, number>() : any
|
||||
>f1 : { <A = any>(): void; <A, B, C, D = any>(): void; }
|
||||
|
||||
f1<number, number, number, number, number>();
|
||||
>f1<number, number, number, number, number>() : any
|
||||
>f1 : { <A = any>(): void; <A, B, C, D = any>(): void; }
|
||||
|
||||
// Overloaded functions with no default type arguments
|
||||
declare function f2<A>(): void;
|
||||
>f2 : { <A>(): void; <A, B, C>(): void; }
|
||||
|
||||
declare function f2<A, B, C>(): void;
|
||||
>f2 : { <A>(): void; <A, B, C>(): void; }
|
||||
|
||||
f2<number, number>();
|
||||
>f2<number, number>() : any
|
||||
>f2 : { <A>(): void; <A, B, C>(): void; }
|
||||
|
||||
f2<number, number, number, number>();
|
||||
>f2<number, number, number, number>() : any
|
||||
>f2 : { <A>(): void; <A, B, C>(): void; }
|
||||
|
||||
// Overloaded non-generic functions
|
||||
declare function f3(): void;
|
||||
>f3 : { (): void; (a: any): void; }
|
||||
|
||||
declare function f3(a): void;
|
||||
>f3 : { (): void; (a: any): void; }
|
||||
>a : any
|
||||
|
||||
f3<number>();
|
||||
>f3<number>() : any
|
||||
>f3 : { (): void; (a: any): void; }
|
||||
|
||||
// Generic function with default type parameters
|
||||
declare function f4<A, B, C = any>(): void;
|
||||
>f4 : <A, B, C = any>() => void
|
||||
|
||||
f4<number>();
|
||||
>f4<number>() : any
|
||||
>f4 : <A, B, C = any>() => void
|
||||
|
||||
f4<number, number, number, number>();
|
||||
>f4<number, number, number, number>() : any
|
||||
>f4 : <A, B, C = any>() => void
|
||||
|
||||
// Generic function with no default type arguments
|
||||
declare function f5<A, B>(): void;
|
||||
>f5 : <A, B>() => void
|
||||
|
||||
f5<number>();
|
||||
>f5<number>() : any
|
||||
>f5 : <A, B>() => void
|
||||
|
||||
f5<number, number, number>();
|
||||
>f5<number, number, number>() : any
|
||||
>f5 : <A, B>() => void
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
tests/cases/compiler/newMap.ts(1,9): error TS2558: Expected 0-2 type arguments, but got 1.
|
||||
tests/cases/compiler/newMap.ts(1,9): error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.
|
||||
|
||||
|
||||
==== tests/cases/compiler/newMap.ts (1 errors) ====
|
||||
new Map<string>();
|
||||
~~~~~~
|
||||
!!! error TS2558: Expected 0-2 type arguments, but got 1.
|
||||
!!! error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(27,5): error TS2345: Argument of type '{}' is not assignable to parameter of type 'number'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(41,11): error TS2345: Argument of type '""' is not assignable to parameter of type 'number'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(63,5): error TS2558: Expected 1-3 type arguments, but got 4.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(63,5): error TS2558: Expected 3 type arguments, but got 4.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(70,21): error TS2345: Argument of type '3' is not assignable to parameter of type 'string'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(71,21): error TS2345: Argument of type '""' is not assignable to parameter of type 'number'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(81,5): error TS2344: Type 'boolean' does not satisfy the constraint 'number'.
|
||||
@@ -79,7 +79,7 @@ tests/cases/conformance/expressions/functionCalls/overloadResolution.ts(91,22):
|
||||
// Generic overloads with differing arity called with type argument count that doesn't match any overload
|
||||
fn3<number, number, number, number>(); // Error
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 1-3 type arguments, but got 4.
|
||||
!!! error TS2558: Expected 3 type arguments, but got 4.
|
||||
|
||||
// Generic overloads with constraints called with type arguments that satisfy the constraints
|
||||
function fn4<T extends string, U extends number>(n: T, m: U);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors.ts(27,9): error TS2345: Argument of type '{}' is not assignable to parameter of type 'number'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors.ts(43,15): error TS2345: Argument of type '""' is not assignable to parameter of type 'number'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors.ts(67,9): error TS2558: Expected 1-3 type arguments, but got 4.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors.ts(67,9): error TS2558: Expected 3 type arguments, but got 4.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors.ts(77,25): error TS2345: Argument of type '3' is not assignable to parameter of type 'string'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors.ts(78,25): error TS2345: Argument of type '""' is not assignable to parameter of type 'number'.
|
||||
tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors.ts(88,9): error TS2344: Type 'boolean' does not satisfy the constraint 'number'.
|
||||
@@ -83,7 +83,7 @@ tests/cases/conformance/expressions/functionCalls/overloadResolutionConstructors
|
||||
// Generic overloads with differing arity called with type argument count that doesn't match any overload
|
||||
new fn3<number, number, number, number>(); // Error
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 1-3 type arguments, but got 4.
|
||||
!!! error TS2558: Expected 3 type arguments, but got 4.
|
||||
|
||||
// Generic overloads with constraints called with type arguments that satisfy the constraints
|
||||
interface fn4 {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(5,11): error TS2558: Expected 0-2 type arguments, but got 3.
|
||||
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(6,15): error TS2558: Expected 0-2 type arguments, but got 3.
|
||||
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(5,11): error TS2558: Expected 2 type arguments, but got 3.
|
||||
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(6,15): error TS2558: Expected 2 type arguments, but got 3.
|
||||
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(9,1): error TS2554: Expected 1 arguments, but got 0.
|
||||
|
||||
|
||||
@@ -10,10 +10,10 @@ tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(9,1): error TS2554:
|
||||
|
||||
Callbacks<number, string, boolean>('s'); // wrong number of type arguments
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 0-2 type arguments, but got 3.
|
||||
!!! error TS2558: Expected 2 type arguments, but got 3.
|
||||
new Callbacks<number, string, boolean>('s'); // wrong number of type arguments
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2558: Expected 0-2 type arguments, but got 3.
|
||||
!!! error TS2558: Expected 2 type arguments, but got 3.
|
||||
|
||||
declare function f<A, B = {}>(arg: number): void;
|
||||
f<number>(); // wrong number of arguments (#25683)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// Overloaded functions with default type arguments
|
||||
declare function f1<A = any>(): void;
|
||||
declare function f1<A, B, C, D = any>(): void;
|
||||
f1<number, number>();
|
||||
f1<number, number, number, number, number>();
|
||||
|
||||
// Overloaded functions with no default type arguments
|
||||
declare function f2<A>(): void;
|
||||
declare function f2<A, B, C>(): void;
|
||||
f2<number, number>();
|
||||
f2<number, number, number, number>();
|
||||
|
||||
// Overloaded non-generic functions
|
||||
declare function f3(): void;
|
||||
declare function f3(a): void;
|
||||
f3<number>();
|
||||
|
||||
// Generic function with default type parameters
|
||||
declare function f4<A, B, C = any>(): void;
|
||||
f4<number>();
|
||||
f4<number, number, number, number>();
|
||||
|
||||
// Generic function with no default type arguments
|
||||
declare function f5<A, B>(): void;
|
||||
f5<number>();
|
||||
f5<number, number, number>();
|
||||
Reference in New Issue
Block a user