The “no overload matches this call” error in TypeScript occurs when you call a function with arguments that don’t match any of its defined overloads. This error is significant because it highlights type mismatches, helping developers catch potential bugs early. It’s commonly encountered when the function’s expected parameters are not correctly provided, either in type or number.
The error message “no overload matches this call overload 1 of 2” in TypeScript means that you are trying to call a function with arguments that do not match any of the defined overloads for that function.
Function overloads allow you to define multiple signatures for a single function. Each signature can have different parameter types or counts. Here’s a simple example:
function example(arg1: string): void;
function example(arg1: number, arg2: string): void;
function example(arg1: any, arg2?: any): void {
// Implementation
}
In this example, example
has two overloads:
string
argument.number
and a string
argument.If you call example(123, 456)
, TypeScript will throw the “no overload matches this call” error because neither overload accepts two number
arguments.
The error occurs because the arguments you provided do not match any of the function’s overloads. To fix it, ensure that the arguments match one of the defined overloads.
The “no overload matches this call” error typically occurs due to:
These are the common reasons behind this error.
Sure, here are specific code examples illustrating the “no overload matches this call overload 1 of 2” error:
function example(str: 'hello'): string;
function example(str: 'world'): string;
function example(str: 'hello' | 'world'): string {
return str;
}
function callExample(str: 'hello' | 'world') {
// ⛔ Error: No overload matches this call.
// Overload 1 of 2 gave the following error.
// Argument of type '"hello" | "world"' is not assignable to parameter of type '"hello"'.
return example(str);
}
function example(str: 'hello'): string;
function example(str: 'world'): string;
function example(str: 'hello' | 'world'): string {
return str;
}
function callExample(str: 'hello' | 'world') {
// ✅ Correct: Using type assertion to match the overload
return example(str as 'hello');
}
const result = ['a', 'b', 'c'].reduce((accumulator: any) => {
// ⛔ Error: No overload matches this call.
// Overload 1 of 3, '(previousValue: string, currentValue: string, currentIndex: number, array: string[]): string', gave the following error.
// Argument of type '() => void' is not assignable to parameter of type '(previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string'.
}, []);
const result = ['a', 'b', 'c'].reduce((accumulator: any, current: string) => {
// ✅ Correct: Providing the correct number of parameters
return accumulator + current;
}, '');
These examples should help illustrate the error and how to correct it.
Check Function Signatures:
Use Type Assertions:
functionName(arg as ExpectedType)
.Add or Modify Overloads:
Review Required Arguments:
These steps should help resolve the error effectively.
The ‘no overload matches this call’, specifically ‘overload 1 of 2’, occurs when TypeScript is unable to find a matching function signature for a given function call. This typically happens due to incorrect argument types, missing required arguments, or incompatible overloads.
To resolve this issue, it’s essential to understand how function overloads work in TypeScript. Overloads allow you to define multiple function signatures with different parameter lists, but all returning the same type. When calling a function, TypeScript will attempt to match the provided arguments against each overload until it finds one that fits.
By understanding how function overloads work in TypeScript and carefully examining the function signature, arguments, and overloads, you can effectively resolve the ‘no overload matches this call’ error.