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;
但爲什麼前者批註失敗?坦率地說,到目前爲止,我還沒有看到函數類型的聯合類型,但是我沒有看到它不應該被允許的理論原因。