1
我正在玩Facebook流和奇怪,为什么以下功能不打字检查?它显然使用由'|'表示的联合类型。Facebook流量功能联盟类型
declare var f: ((x: any) => number) | ((x: any) => string);
function f(x) {
if(true) {
return 5;
}
else return 'hello';
}
检查者抱怨:
function
This type is incompatible with
union type
我知道,当我将其标注为喜欢的作品:
declare var f: (x: any) => number|string;
但为什么前者批注失败?坦率地说,到目前为止,我还没有看到函数类型的联合类型,但是我没有看到它不应该被允许的理论原因。