I have a map function:
const map = <T, U>(f: (x: T) => U, arr: T[]): U[] => { return arr.map((val) => f(val)); }
When I call map with an anonymous function as a callback, the return type is correct:
// `x1` variable type here is { name: string }[], which is correct const x1 = map(x => x, [{name: 'John'}]);
But when I provide an identity function instead of an anonymous one, the return type is incorrect:
const identity = <T>(x: T) => x // return type of `x2` is {}[] here const x2 = map(identity, [{name: 'John'}]);
How to get the correct type result for the second example, without providing explicit type arguments for the map function?
typescript
1ven
source share