I have map function:
const map = <T, U>(f: (x: T) => U, arr: T[]): U[] => {
return arr.map((val) => f(val));
}
When I'm calling map with anonymous function as a callback, it's return type is correct:
// `x1` variable type here is { name: string }[], which is correct
const x1 = map(x => x, [{name: 'John'}]);
But when I'm providing identity function instead of anonymous one, return type is wrong:
const identity = <T>(x: T) => x
// return type of `x2` is {}[] here
const x2 = map(identity, [{name: 'John'}]);
How to get correct type result for 2nd example, without providing explicit type arguments for map function?
Tonidentity?map()'s arguments. I guess it forces the compiler to match the array element type first? I don't know if there's a way to get the inference to work in the order you have.