2017-05-29 87 views
0

我遇到了流量錯誤,同時基本上從flow docs做基本的減速比例子。redux減速器流量不推斷類型

來自流程的錯誤添加到代碼中:REMOVE切換案例:action未解析爲正確的類型。

如果我懸停在​​在vscode,在它顯示爲AddActionADD情況,但是上殼體REMOVE它顯示爲所有動作的並集,即Action

我在想什麼或理解錯誤?流程應從Actions聯合中扣除正確的類型,以便將ifswitch以內的唯一可能的類型扣除。

// @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; 

回答

2

好型推斷的作品,看來這個問題是使用action裏面Array.filter箭頭功能:

如果我

case 'REMOVE': { 
    const id = action.payload.id; 
    return state.filter(t => t.id !== id); 
} 

錯誤消失更換REMOVE情況的內容。

我想流量不能推斷出箭頭函數裏面的類型。知道爲什麼會很有趣。

編輯:related question

,那麼流程,因爲它假設過濾無效工會細化()可能會做的副作用減速參數actiondocs)。使用前將操作或有效負載存儲在const中修復了此問題。