TLDR: 我在Try Flow(link)中建模了我的问题 . 你能帮我解决一下吗?
Full description of the problem:
在Redux reducer中,我选择了三个动作 . 一个动作可以具有任何类型,但其独特的特征是它在其有效载荷中具有特定字段(在该示例中为 payload.entities.items
) . 另外两个动作具有特定的类型来区分它们,并且具有不同的有效载荷:
const FOO = 'FOO'
const BAR = 'BAR'
type ActionWithEntities = {|
type: string,
payload: {|
entities: {|
items: {
[string]: string
}
|}
|}
|}
type ActionWithFoo = {|
type: typeof FOO,
payload: {|
foo: string
|}
|}
type ActionWithBar = {|
type: typeof BAR,
payload: {|
bar: string
|}
|}
type Action =
| ActionWithEntities
| ActionWithFoo
| ActionWithBar
根据动作,我想在reducer中做任何适当的事情(并不重要):
function reducer(state: State, action: Action) {
if (action.payload && action.payload.entities && action.payload.entities.items) {
return Object.assign({}, state, action.payload.entities.items);
}
switch(action.type) {
case FOO:
const foo = action.payload.foo;
return { foo }
case BAR: {
const bar = action.payload.bar;
return { bar }
}
default:
return state;
}
}
我的问题是,没有按流程't see that the if-statement takes care of the first action (because other actions don'吨有其有效载荷的 entities
场),并抱怨switch语句是,我试图用现场( foo
或 bar
在这个例子中)是从第一个动作缺席里面 .
让Flow满意的正确方法是什么?
1 回答
流不同的原因是因为ActionWithEntities的类型字段是string类型,这意味着它可以具有值FOO或BAR . 现在,您的逻辑将阻止它命中交换机/案例,但流程无法遵循该逻辑并改进类型 . 解决它的一种方法是显式键入ActionWithEntities的类型字段
所以像