Encountering an issue with the code below while attempting to create a proxy for a function with multiple overloads:
// The target function
function original (a: number): boolean;
function original (a: string): boolean;
function original (a: boolean): boolean;
function original (a: number | string | boolean): boolean {
return true;
}
// The proxy
function pass (a: string | number){
return original(a); // here
};
pass(5);
pass('a');
pass(true);
When trying to create a proxy for the original
function, TypeScript throws the following error :
No overload matches this call.
Overload 1 of 3, '(a: number): boolean', gave the following error.
Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
Overload 2 of 3, '(a: string): boolean', gave the following error.
Argument of type 'string | number' is not assignable to parameter of type 'string'.
Type 'number' is not assignable to type 'string'.
Overload 3 of 3, '(a: boolean): boolean', gave the following error.
Argument of type 'string | number' is not assignable to parameter of type 'boolean'.
Type 'string' is not assignable to type 'boolean'.(2769)
input.tsx(5, 10): The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.
Key points to consider about this situation :
- I've simplified the scenario to isolate when the problem occurs
- The
pass(true)
call results in an error, indicating that typing seems to work for thepass
function - In my specific case, the
original
function comes from a module, so any solution without modifying it would be ideal
View a TS Playground with the aforementioned code.