Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CombineReducers with Typescript returns error "Argument of type is not assignable to parameter of type 'ReducersMapObject'"

I'm trying to setup a front-end environment with react + redux + typescript, but I am struggling to get it work with combineReducers. I get an error: Argument of type is not assignable to parameter of type 'ReducersMapObject'. See the full error message below the code.

STATE: (types/index.tsx)

export namespace StoreState {

  export type Enthusiasm = {
    languageName: string;
    enthusiasmLevel: number;
  }

  export type All = {
    enthusiasm: Enthusiasm
  }
}

STORE: (store.tsx)

import { createStore } from 'redux';
import reducers from './reducers/index';
import { StoreState } from './types/index';

let devtools: any = window['devToolsExtension'] ? window['devToolsExtension']() : (f:any)=>f;

const Store = createStore<StoreState.All>(reducers, devtools);
export default Store;

REDUCER: (/reducers/HelloReducer.tsx)

    import { EnthusiasmAction } from '../actions';
    import { StoreState } from '../types/index';
    import { INCREMENT_ENTHUSIASM, DECREMENT_ENTHUSIASM } from '../constants/index';

    export const enthusiasm = (state: StoreState.Enthusiasm, 
action: EnthusiasmAction): StoreState.Enthusiasm => {
      switch (action.type) {
        case INCREMENT_ENTHUSIASM:
            return { ...state, enthusiasmLevel: state.enthusiasmLevel + 1 };
        case DECREMENT_ENTHUSIASM:
            return { ...state, enthusiasmLevel: Math.max(1, state.enthusiasmLevel - 1) };
        default:
            return state;
     }
   }

COMBINE REDUCERS (/reducers/index.tsx)

import { StoreState } from '../types/index';
import * as enthusiasmReducer from './HelloReducer';

import { combineReducers } from 'redux';

const reducer = combineReducers<StoreState.All>({
    enthusiasm: enthusiasmReducer
});

export default reducer;

enter image description here

like image 536
podeig Avatar asked Nov 13 '17 14:11

podeig


1 Answers

You're passing the object with all of HelloReducer's exports instead of just the reducer. There's a couple of ways to fix it. You can select the reducer:

const reducer = combineReducers<StoreState.All>({
    enthusiasm: enthusiasmReducer.enthusiasm
});

or import only the reducer:

import {enthusiasm} from './HelloReducer';
..
const reducer = combineReducers({enthusiasm});

or add export default enthusiasm; to HelloReducer.tsx and change the import to

import enthusiasmReducer from './HelloReducer';
like image 154
Oblosys Avatar answered Sep 30 '22 10:09

Oblosys