rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.95k stars 866 forks source link

Argument of type 'Reducer<{}>' is not assignable to parameter of type 'BaseReducer<{}, AnyAction>' #753

Open Icehunter opened 6 years ago

Icehunter commented 6 years ago
ERROR in [at-loader] ./ClientApp/application.tsx:38:4 
    TS2345: Argument of type 'Reducer<{}>' is not assignable to parameter of type 'BaseReducer<{}, AnyAction>'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'void | {}' is not assignable to type '{}'.
      Type 'void' is not assignable to type '{}'.

Relevant Code:

store.ts

import { combineReducers } from 'redux';

import * as User from './User';

export interface ApplicationState {
  user: User.UserState;
}

export const reducers = combineReducers({
  user: User.reducer
});

application.tsx

import { reducers } from './store';

const reducer = combineReducers(Object.assign({}, {
  store: reducers
}, {
  routing: routerReducer
}));

const persistedReducer = persistReducer({
  key: 'root',
  storage: storage,
}, reducer);

const store = compose(applyMiddleware(
  middleware,
  thunk,
  logger
))(createStore)(persistedReducer);

const persistedStore = persistStore(store);

My User reducer is exported as follows:

export const reducer: Reducer<UserState> = (state: UserState, action: any) => {}

Using 5.5.0 with typescript 2.6.2 produces no error; but if I update to 5.9.1; I get this one. I'm really disliking typescript updates/usage lately...

dawnmist commented 6 years ago

The same issue occurs with the definition of persistReducer. The reducer function returned is missing the | undefined for the state, so redux 4.0 rejects the persistedReducer when you try to create the store from it.

Since you already have a pull request for updating this bit, do you want to add the fix for the persistReducer function too?

    export function persistReducer<S, A>(config: PersistConfig, baseReducer: BaseReducer<S, A>): (s: S | undefined, a: A) => S & PersistPartial;