联合类型未在流类型中正确推断
Union Types are not inferred properly in flowtype
flowtype 无法识别以下给定类型,当我将它们用作如下函数的参数时:
/* @flow */
type Action =
{ type: "SELECT", componentToSelect: string }
| { type: "UPDATE", componentToUpdate: string };
function dispatch(action: Action) {
console.log(action.componentToSelect);
}
dispatch({
type: "SELECT",
componentToSelect: "anything"
});
报错信息如下:
componentToSelect
. Property not found in object type
代码在线here
dispacth 函数需要一个 Action,但无法确定该 action 是否具有 属性 componentToSelect。
我应该先检查类型。
function dispatch(action: Action) {
if (action.type === 'SELECT')
console.log(action.componentToSelect);
}
查看online
flowtype 无法识别以下给定类型,当我将它们用作如下函数的参数时:
/* @flow */
type Action =
{ type: "SELECT", componentToSelect: string }
| { type: "UPDATE", componentToUpdate: string };
function dispatch(action: Action) {
console.log(action.componentToSelect);
}
dispatch({
type: "SELECT",
componentToSelect: "anything"
});
报错信息如下:
componentToSelect
. Property not found in object type
代码在线here
dispacth 函数需要一个 Action,但无法确定该 action 是否具有 属性 componentToSelect。 我应该先检查类型。
function dispatch(action: Action) {
if (action.type === 'SELECT')
console.log(action.componentToSelect);
}
查看online