I would like to create a fat-arrow function, that will return:
- string, if the argument is a number,
- number, if the argument is a string.
This is my code so far:
type ToString = (input: number) => string
type ToNumber = (input: string) => number
type Converter = ToString | ToNumber
const convert: Converter = <T extends number | string>(val: T) => null as any // The function body is not important.
const x = convert(7) // Error: Argument of type 'number' is not assignable to parameter of type 'never'. (2345)
Unfortunately, the code above doesn't work because of the mentioned error message on the last line. Is there any way to fix it?