I attempted to "overload" a function by defining it as a union function type in order to have the type of the input parameter dictate the type of the `data` property in the returned object. However, this resulted in an error:
type FN1 = (a: string) => {data: string};
type FN2 = (b: number) => {data: number};
const fn: FN1 | FN2 = c => ({data: c});
fn('some string');
^^^^^^^^^^^^^ <= Argument of type 'string' is not assignable to parameter of type 'never'.
The variable fn
is inferred as
const fn: (arg0: never) => {data: string} | {data: number}
Why is the parameter being assigned as type never
? Is there an alternative approach to achieve this?