Redux操作的流程 - 在对象类型中找不到属性

时间:2017-09-27 20:50:04

标签: redux flowtype

在使用Flow的类型检查缩减器中,当我的操作返回时:

{ type: TYPE_1 }

{ type: TYPE_2, data: { key: value } }

在尝试合并两个动作时,如何避免“在对象文字中找不到属性”:

type Action = { type: string } | { type: string, data: Object };

1 个答案:

答案 0 :(得分:2)

最好的做法是更具体地说明哪些行动可能发生:

type Action1 = { type: 'TYPE_1' };
type Action2 = { type: 'TYPE_2', data: Object };

type Action =
  | Action1
  | Action2;

我还会在减速器内部指定data的类型如果更重要。

参考:https://flow.org/en/docs/react/redux/#toc-typing-redux-actions

相关问题