Typescript does not select the correct overload based on return callback type

Given the following code (linkground link: http://bit.ly/1n7Fcow )

declare function pick<T, V>(f:(x:T, y:V) => V):V; declare function pick<T, V>(f:(x:T, y:V) => T):T; var xx = pick((x: number, y:string) => x); var yy = pick((x: number, y:string) => y); 

TypeScript selects the wrong overload and does not output the xx type.

Is it possible to make typescript select the correct overload there?

Note: to avoid the XY problem, this is the original problem - http://bit.ly/QXaQGc - I need such an overload to be able to model promises correctly.

+6
source share
1 answer

I was able to get a playground to correctly determine the correct type of xx and yy, changing the call to this:

 declare function pick<T, V>( f:(x:T, y:V) => V ):V; declare function pick<T, V>( f:(x:T, y:V) => T ):T; var xx = pick<number,string>((x, y) => x); var yx = pick<number,string>((x, y) => y); 

See here: http://bit.ly/1p6h8iP I hope for this help.

+1
source

Source: https://habr.com/ru/post/969475/


All Articles