我在执行flow docs 中的基本 redux-reducer 示例时遇到了流程错误。
此处添加到代码中的流程错误:在REMOVE
switch 案例中:action
未解析为正确的类型。
如果我将鼠标悬停payload
在 vscode 中,在这种ADD
情况下它显示为AddAction
,但在REMOVE
情况下它显示为所有操作的联合,即Action
.
我错过了什么或理解错误?流量应扣除正确的类型从下Actions
工会的唯一可能的类型中if
和switch
。
// @flow
const initialState = [];
type Item = { id: number, data: string };
type State = Item[];
type AddAction = {
type: 'ADD',
payload: Item
};
type RemoveAction = {
type: 'REMOVE',
payload: { id: number }
};
type ClearAction = {
type: 'CLEAR'
};
type Action = AddAction | RemoveAction | ClearAction;
const reducer: (State, Action) => State = (state = initialState, action) => {
switch (action.type) {
case 'ADD': {
return [...state, action.payload];
}
case 'REMOVE': {
return state.filter(t => t.id !== action.payload.id);
^ property `payload`. Property not found in
}
case 'CLEAR': {
return [];
}
default:
(action: empty);
return state;
}
};
export default reducer;
尝试流程上的代码
另一个try-flow repl,我基本上做同样的事情,类型推断按预期工作
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句