在使用CombineReducers创建rootReducer时出现问题

时间:2019-07-15 16:00:58

标签: javascript reactjs typescript react-redux react-router-redux

import { combineReducers } from 'redux';
import { reducers } from './reducers';
import { IAppAction } from './action';
import { routerReducer } from 'react-router-redux';
import { IAppState } from './state/app-state';

const appReducer = combineReducers({
    ...reducers,
    router: routerReducer
});

export const rootReducer = (state: IAppState, action: IAppAction) => {
    return appReducer(state, action)
};

我遇到以下错误

"Argument of type 'IAppState' is not assignable to parameter of type '{ router: RouterState; app: any; }'.
  Type 'IAppState' is missing the following properties from type '{ router: RouterState; app: any; }': router, app"

将示例上传到github

软件包版本

"react": "^16.8.6",
"redux": "^4.0.4",
"react-router-redux": "^4.0.8",
"typescript": "3.5.3"

与以前的项目完全相同的代码

"react": "^16.8.4",
"redux": "^3.7.2",
"react-router-redux": "^4.0.8",
"typescript": "^3.3.4000"

1 个答案:

答案 0 :(得分:0)

更简单的解决方法是,您需要在IAppState中添加缺少的类型,以根据您的CombineReducers代码添加所需的路由器,而我认为应该是您的app: any的{​​{1}}的IAppState

可以创建以更好地管理它,并且不会根据用例/代码污染您的IAppState,在您可能不需要路由器的地方

any

您的rootReducer变为

interface IAppStateWithRouter extends IAppState = { router: RouterState }