x89
x89

Reputation: 3460

CombineReducers in TypeScript

export const rootReducer = combineReducers({
    login: loginReducer,
});

This works fine but as soon as I try to combine another reducer,

export const rootReducer = combineReducers({
    login: loginReducer,
    logout: logoutReducer
});

I start getting an error on rootReducer that

'rootReducer' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

How could I modify this?

This is how my logoutReducer looks like:

import {store} from '../index'

export const logoutReducer = (state = store.getState(), { type, payload } :any) => {
  switch (type) {
    case "logout":
      return {...state, token: payload};
    default:
      return state;
  }
};

Upvotes: 1

Views: 3158

Answers (1)

abest
abest

Reputation: 494

Have you tried this with assigning type to your underlying reducers?

For example:

import {Action, Reducer} from 'redux';
interface LoginState {
   isLoggedIn: boolean;
   token: string;
}

interface LogoutState { 
   token:string;
}

export const logOutReducer: Reducer<LogoutState> = (state: LogoutState | undefined, incomingAction: Action): LogoutState=> {
switch (incomingAction.type) {
    case "logout":
      return {...state, token: payload};
    default:
      return state;
  }
}
//... export const logInReducer: Reducer<LoginState>...

Upvotes: 1

Related Questions