0
我遇到了流量錯誤,同時基本上從flow docs做基本的減速比例子。redux減速器流量不推斷類型
來自流程的錯誤添加到代碼中:REMOVE
切換案例:action
未解析爲正確的類型。
如果我懸停在在vscode,在它顯示爲AddAction
的ADD
情況,但是上殼體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
another try-flow repl在那裏我基本上做同樣的事情,並如預期