简体   繁体   English

Typescript没有根据回调返回类型选择正确的重载

[英]Typescript doesn't select the correct overload based on callback return type

Given the following code (playground link: http://bit.ly/1n7Fcow ) 给出以下代码(游乐场链接: 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 an incorrect overload and fails to infer the type of xx . TypeScript选择了不正确的重载,无法推断xx的类型。

Is it possible to make typescript select the right overload there? 是否有可能使打字稿选择正确的超载?

note: to avoid the XY problem, this is the original problem - http://bit.ly/QXaQGc - I need this kind of overloading in order to be able to model promises correctly. 注意:为了避免XY问题,这是最初的问题 - http://bit.ly/QXaQGc - 我需要这种重载才能正确建模promises。

I was able to get the playground to infer the correct type of xx and yy correctly by changing the call to this: 通过更改对此的调用,我能够让操场正确地推断出正确的xx和yy类型:

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 Hope this help. 看到这里: http//bit.ly/1p6h8iP希望这个帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM