Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return type for redux root reducer

I am convert my root reducer from javascript to typescript, however, I am getting a linting error saying:

Missing return type on function. (@typescript-eslint/explicit-function-return-type)

What should be the proper return type for my root reducer function? Thanks.

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),
});
like image 506
Jimmy Avatar asked Jul 25 '26 12:07

Jimmy


1 Answers

It seems that you've set the rule "Require explicit return types on functions and class methods" and you're required to specify return types for functions explicitly.

combineReducer return function of type Reducer<S, A>, where S is standing for app state.

First of all I suggest you to define app state like below

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

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

Then you'll be able to type root reducer

import { Reducer } from 'redux';

export default (historyObject: History): Reducer<AppState> => combineReducers({
    sampleReducer,
    router: connectRouter(historyObject),
});
like image 120
Fyodor Avatar answered Jul 27 '26 13:07

Fyodor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!