rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.91k stars 862 forks source link

Typescript + createTransform #1373

Closed jayesbe closed 2 years ago

jayesbe commented 2 years ago

When using createTransform, something happens with the types such that any selectors i've created cannot no longer detect the types defined for the State

ie, this works

const persistConfig = {
    key: "root",
    storage,
};

this however, does not

const persistConfig = {
    key: "root",
    storage,
    transforms: [ routesTransform ],
};

Typescript immediately starts complaining

the routesTransform looks like this

export const routesTransform = createTransform(
  (inboundState: RouterState) => {
    return inboundState;
  },
  (outboundState: RouterState) => {
    return outboundState;
  },
  { whitelist: ['routerState'] }
);

There is no actual transform occurring.. why is typescript complaining ?

jayesbe commented 2 years ago

Fixed as per https://github.com/rt2zz/redux-persist/issues/1140#issuecomment-1034172681

Only change I had to make was one line

const persistedReducer = persistReducer(persistConfig, rootReducer);

to

const persistedReducer = persistReducer(persistConfig, rootReducer) as typeof rootReducer;