3

我正在将我的 root reducer 从 javascript 转换为 typescript,但是,我收到一个 linting 错误说:

缺少函数的返回类型。(@typescript-eslint/explicit-function-return-type)

我的根 reducer 函数的正确返回类型应该是什么?谢谢。

import { History } from 'history';
import { combineReducers } from 'redux';
import { connectRouter } from 'connected-react-router';
import sampleReducer from './sampleReducer';

export default (historyObject: History) => combineReducers({
  sampleReducer,
  router: connectRouter(historyObject),
});
4

1 回答 1

2

似乎您已经设置了“要求函数和类方法显式返回类型”的规则,并且您需要显式指定函数的返回类型。

combineReducer类型的返回函数Reducer<S, A>,其中S代表应用状态。

首先,我建议您定义应用程序状态,如下所示

import { RouterState } from 'connected-react-router';

type AppState = {
    sampleReducer: /* type of state, that sampleReducer accepts and returns */;
    router: RouterState
}

然后你就可以输入 root reducer

import { Reducer } from 'redux';

export default (historyObject: History): Reducer<AppState> => combineReducers({
    sampleReducer,
    router: connectRouter(historyObject),
});
于 2019-07-18T03:28:54.730 回答