我最近按照Dan在这里提供的建议,为Redux商店实施了注销操作:
https://stackoverflow.com/a/35641992/11980562
但是,既然我已将减速器分为intervals_days
和rootReducer
,我的appReducer
会引发以下错误:
createStore
Argument of type '(state: { activity: ActivityState; asyncActions: AsyncActionState; tasks: TaskState; userInfo: UserInfoState; }, action: RootAction) => { activity: ActivityState; asyncActions: AsyncActionState; tasks: TaskState; userInfo: UserInfoState; }' is not assignable to parameter of type 'Reducer<{ activity: ActivityState; asyncActions: AsyncActionState; tasks: TaskState; userInfo: UserInfoState; }, RootAction>'.\n Types of parameters 'state' and 'state' are incompatible.\n Type '{ activity: ActivityState; asyncActions: AsyncActionState; tasks: TaskState; userInfo: UserInfoState; } | undefined' is not assignable to type '{ activity: ActivityState; asyncActions: AsyncActionState; tasks: TaskState; userInfo: UserInfoState; }'.\n Type 'undefined' is not assignable to type '{ activity: ActivityState; asyncActions: AsyncActionState; tasks: TaskState; userInfo: UserInfoState; }
我不太清楚为什么引入这种新行为会导致状态不确定。
我还注意到,当
// reducers/index.ts
const initialRootState: RootState = {
activity: initialActivityState,
asyncActions: {},
tasks: initialTaskState,
userInfo: initialUserState
};
export const appReducer = combineReducers({
activity: activityReducer,
asyncActions: asyncActionReducer,
tasks: tasksReducer,
userInfo: userReducer
});
export default (state: RootState, action: RootAction) => {
return appReducer(
action.type === LogoutActionType ? initialRootState : state,
action
);
};
// store/index.ts
export const store = createStore(rootReducer, initialState, enhancers);
仅调用rootReducer
而没有新的appReducer
行为时,同样的错误仍然存在:
LogoutActionType
任何想法都将不胜感激!
答案 0 :(得分:0)
根据official type definition,Reducer
类型的函数应接受undefined作为第一个参数,而导出的函数则不是这种情况。
编写单个化合器时未遇到此错误的原因很可能是它们具有以下形式:
function subReducer(state: SubState = { dummy: '1' }, action: any) {
return state;
}
第一个参数带有默认值,该默认值还将函数的类型签名从(state: SubState, action: any)
更改为(state: SubState | undefined, action: any)
。
如果第一个参数没有默认值,则必须手动更改其类型声明,并添加undefined
以形成联合类型。