kmm3
kmm3

Reputation: 391

In Typescripted Redux, how do I find my root reducer's type?

export const makeStore = () => {
  const store = configureStore({
    reducer: {
      reducer,
      growthReportSlice,
      selectBarSlice,
    },
    ....
  return store;
};
const dummy = makeStore();

export const wrapper = createWrapper(makeStore, { debug: true });
export type AppDispatch = typeof dummy.dispatch;
// export type RootReducer = ReturnType<typeof reducer> &
//   ReturnType<typeof growthReportSlice> &
//   ReturnType<typeof selectBarSlice>;
export const useAppDispatch = () => useDispatch<AppDispatch>();

I just need the ReturnType of the root reducer but I have 3 reducers combined. How can I get the correct typing?

Upvotes: 0

Views: 47

Answers (1)

Orinayo Oyelade
Orinayo Oyelade

Reputation: 347

You're almost there, you just need to combine your reducers.

import {combineReducers} from 'redux'
const rootReducer = combineReducers({
  reducer,
  growthReportsSlice,
  selectBarSlice
})

type RootState = ReturnType<typeof rootReducer>

...
const store = configureStore({
    reducer: rootReducer,
...

Upvotes: 2

Related Questions