使用类型安全操作创建根减少器



我正在使用typesafe-actions,并希望创建一个根减少器,使我能够处理像LOGGED_OUT这样的全局操作,以便清除状态。就像这个问题的答案一样。

我尝试了一些方法,但我一直失去typesafe-actions提供的类型安全性,并且reducer恢复为any类型。

我有一个非常普通的安全操作设置。我还没有发布我尝试过的隐式打字的排列解决方案,因为我认为这不会增加问题的清晰度。

我能找到的最接近解决方案是在这个线程中。类型安全操作的维护者自己发布了一条回复,并提到他已经有了一个解决方案,尽管从那时起就没有提供任何链接或引用。

任何帮助都会很棒。

为了记录在案,我可以按照我发布的堆栈溢出问题的模式使其在功能上发挥作用,但类型已损坏。

原来我9个月前已经在一个单独的项目中解决了这个问题,但忘记了。

import { combineReducers } from 'redux';
import auth from '../modules/auth/reducer';
import feed from '../modules/feed/reducer';
import post from '../modules/post/reducer';
import profile from '../modules/profile/reducer';
import { StateType, Reducer, RootAction } from 'typesafe-actions';
import { signOut } from 'modules/auth/actions';
const appReducer = combineReducers({
auth,
feed,
post,
profile
});
type RootState = StateType<typeof appReducer>;
const clearOnSignOutReducer: Reducer<RootState, RootAction> = (
state,
action
) => {
if (action.type === signOut().type) {
state = undefined;
}
return appReducer(state, action);
};
export default clearOnSignOutReducer;

最新更新