打字稿React-Redux动作更新多个Reducer

时间:2020-06-08 21:31:31

标签: typescript react-redux

这与此特定的answer有关。但是我遇到打字稿的麻烦,因为有效载荷有不同的参数: 我有减速板

const board = (state: IBorardState, action: AppActionTypes): IBorardState => {

    switch (action.type) {

        case ActionTypes.ADD_LIST: {
            const { listId } = action.payload;
            return { lists: [...state.lists, listId] };
        }

列表减速器

export const listsById = (state: IListsState = {}, action: AppActionTypes): IListsState => {
    switch (action.type) {

        case ActionTypes.ADD_LIST: {
            const { listId, listTitle } = action.payload;
            return {
                ...state,
                [listId]: {
                    _id: listId,
                    title: listTitle
                }
            }
        }

但是我尝试创建操作类型:

export const addList = (listId: string, listTitle?: string): AppActionTypes => {
  return {
    type: ActionTypes.ADD_LIST,
    payload: {listId, listTitle}
  }
}

并调度以下操作:

store.dispatch({
    type: ActionTypes.ADD_LIST,
    payload: { listId: firstListId, listTitle: "First List"}
});

由于可空参数,Typescript不会同时触发这两种情况

感谢您的帮助。

0 个答案:

没有答案