2017-04-23 56 views
0

我可以鍵入我的動作,我的初始狀態和普通縮減器,但是當reducer返回一個包含lodash的狀態片段時,Flow似乎並未檢測到它:Flow:如何使用帶有Redux減速器和lodash的JS Flow類型?

setup:

// @flow 
import _ from 'lodash' 
import constants from './constants'; 

type initialStateType = { 
    authenticating: boolean, 
    authenticated: boolean, 
    goals: Array<?Object>, 
    user: Object 
} 

type genericAction = { 
    type: string, 
    goals?: Array<?Object>, 
    data?: Object | Array<mixed>, 
    user?: Object, 
    goalId?: ?number, 
    status?: boolean, 
} 

export const initialState: initialStateType = { 
    authenticating: false, 
    authenticated: false, 
    goals: [], 
    user: {}, 
}; 

減速的情況下,隨着流量的工作原理:

export function globalReducer(state: initialStateType = initialState, action: genericAction) { 
    switch (action.type) { 
    // Authentication 
    case constants.AUTHENTICATING: 
     return Object.assign({}, state, 
     { 
      authenticating: action.status, 
     } 
    ); 
... 
    case constants.USER_EDITED: 
    case constants.USER_FETCHED: 
     return Object.assign({}, state, 
     { 
      user: action.user, 
     } 
    ); 

減速的情況下,不與流動工作:

case constants.GOAL_CREATED: 
     return Object.assign({}, state, 
     { 
      goals: _.concat(state.goals, action.data), 
     } 
    ); 
... 
case constants.GOAL_DELETED: 
    return Object.assign({}, state, 
    { 
     goals: _.filter(state.goals, (value, index) => { 
     return value.id !== action.goalId; 
     }), 
    } 
); 

使用Nuclide作爲我的文本編輯器,內置流程支持,流程強調了一行,'goals:_.concat(state.goals,action.data)',帶有一條黃色消息,指出類型覆蓋率,不包括流。

如何使用lodash這些代碼行獲得正確的流覆蓋率?

Type error from Flow

回答

1

您需要安裝庫定義爲lodash,否則流量無法知道它做什麼的方式。

最簡單的方法是通過flow-typed。該頁面上的說明應該足以讓你開始。基本上,它是一種爲npm包查找用戶提供的庫定義並將它們放入存儲庫以便Flow可以使用它們的工具。