Currently, I am in the process of developing a function that invokes another function with enums as accepted parameters. The return type from this function varies depending on the value passed. Both the function being called (b) and the calling function (a) are required to have their parameters adhere to the enum. I am perplexed as to why the provided code is generating a TypeScript error.
export function a (mode: 'a' | 'b') {
return b(mode)
}
export function b (mode: 'a'): string
export function b (mode: 'b'): number
export function b (mode: 'a' | 'b'): string | number {
return (mode=='a') ? 'string' : 1;
}
Error:
No overload matches this call.
Overload 1 of 2, '(mode: "a"): string', gave the following error.
Argument of type '"a" | "b"' is not assignable to parameter of type '"a"'.
Type '"b"' is not assignable to type '"a"'.
Overload 2 of 2, '(mode: "b"): number', gave the following error.
Argument of type '"a" | "b"' is not assignable to parameter of type '"b"'.
Type '"a"' is not assignable to type '"b"'.ts(2769)
Cart.tsx(118, 18): The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.
A similar question can be found here, however, I aim to avoid having a signature that is ambiguous - where mode 'a' always returns a string, and 'b' always returns a number.