【发布时间】:2018-10-31 00:13:13
【问题描述】:
我正在尝试学习如何使用 Flow,但我被困在这里。
我有一个接受不同类型操作的 redux reducer。每个动作的动作载荷都不同,例如:
const CreateNewTaskAction = (n: number) => {
return {type: "CREATE", payload: n};
}
const AnotherAction = (r: TaskHandlerType) => {
return {type: "WHATEVER", payload: r}
}
动作载荷的类型如下:
type TaskHandlerType = {
...
childId: number,
parentId: number,
}
export type ActionType = {
type: string,
payload?: string | number | TaskHandlerType,
}
这是我的减速器的一部分:
const MyReducer = (state: StateType, action: ActionType) = {
switch(action.type) {
...
case "WHATEVER":
....
var newChildrenArray = oldChildrenArray.filter((el) => {
return el != action.payload.childId;
})
....
}
}
这是 Flow 吐出的错误:
Cannot get action.payload.childId because:
• property childId is missing in String [1].
• property childId is missing in undefined [2].
• property childId is missing in Number [3].
src/redux/reducers/tasksReducer.js
57│ var oldChildrenArray = state[parentId].childrenIds
58│ var elementIndex = oldChildrenArray.indexOf(action.payload)
59│ var newChildrenArray = oldChildrenArray.filter((el) => {
60│ return el != action.payload.childId;
61│ })
62│ var newParentState = {
63│ ...state[parentId],
src/redux/actions/actionType.js
[1][2][3] 17│ payload?: string | number | TaskHandlerType ,
它正在检查动作负载是字符串或数字的情况,并警告这些类型没有属性 childId,但这就是我使用 switch 语句的原因,在该分支上,action.payload 将始终具有 childId。
那我怎么告诉 Flow 呢?
【问题讨论】:
-
出于好奇,您使用的是哪个版本的 Flow?
-
0.75 + 11 个字符
标签: javascript redux flowtype