"No overload matches this call" in TypeScript
If you're a TypeScript developer, you've likely encountered the "No overload matches this call" error at some point. This guide aims to shed light on why this error occurs and how to resolve it.
Understanding the Error
The "No overload matches this call" error occurs when you try to call a function (or a method) that has multiple overload signatures, but none of these overloads match the arguments you provided.
Common Causes
Mismatched Arguments
The most straightforward cause is passing arguments of the wrong type or the wrong number of arguments.
function example(arg1: string, arg2: number): void; function example(arg1: number, arg2: string): void; example(123, 456); // Error: No overload matches this call
In the above example, there are two overloads for the example
function. The first expects a string
followed by a number
, and the second expects a number
followed by a string
. Neither of these overloads matches the (123, 456)
call.
Incorrect Context
Sometimes, the error arises when the function expects its context (this
) to be of a certain type.
class MyClass { prop: string; constructor() { ['a', 'b'].forEach(this.handle); // Error: No overload matches this call } handle(item: string, index: number) { console.log(this.prop, item); } }
Here, Array.prototype.forEach
expects a callback of type (value: T, index: number, array: T[]) => void
. But when handle
is passed to forEach
, TypeScript assumes that this
inside handle
can be anything, which could lead to runtime errors.
Solutions
Correct the Arguments
Ensure the arguments you're passing match one of the function's overload signatures.
example('123', 456); // OK
Binding the Function Context
If the error is due to a context issue, bind the function to the correct context:
['a', 'b'].forEach(this.handle.bind(this)); // OK
Or use an arrow function to maintain the correct context:
['a', 'b'].forEach((item, index) => this.handle(item, index)); // OK
Extend Overload Signatures
If you're in control of the function's declaration, and the missing overload is legitimate, add the required overload:
function example(arg1: string, arg2: number): void; function example(arg1: number, arg2: string): void; function example(arg1: number, arg2: number): void; example(123, 456); // OK
Utilize Type Assertions
Sometimes, you're sure of the types you're working with, but TypeScript isn't. In such cases, you can use type assertions:
example(123 as any, 456 as any); // Not recommended unless necessary
Note: Use type assertions with caution. Bypassing TypeScript's type safety defeats the purpose of using TypeScript in the first place.
Check Generics
Generics can also cause overload issues. Ensure that if the function or method is generic, you're either providing the generic type or allowing TypeScript to correctly infer it.
function genericExample<T>(arg: T): T { return arg; } genericExample<number>(123); // OK genericExample('hello'); // OK, string is inferred
Tooling and Resources
- TypeScript Playground: Before making changes to your code, you can try to reproduce the issue in the TypeScript Playground. This will help you isolate the problem and test potential solutions.
- TypeScript Docs: The official TypeScript documentation is a valuable resource for understanding function overloads and type checking in depth.
By understanding the underlying causes of the "No overload matches this call" error and leveraging the provided solutions, you can write safer, cleaner TypeScript code.
Invite only
We're building the next generation of data visualization.
How to turn webpages into editable canvases with a JavaScript bookmarklet
Kris Lachance
How to fix the "not all code paths return a value" issue in TypeScript
Kris Lachance
Working with WebSockets in Node.js using TypeScript
Kris Lachance
Type Annotations Can Only Be Used in TypeScript Files
Kris Lachance
Guide to TypeScript Recursive Type
Kris Lachance
How to Configure Knex.js with TypeScript
Kris Lachance