question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Describe inferring RootState type from store.getState

See original GitHub issue

The docs show inferring type RootState = ReturnType<typeof rootReducer>. But, if you pass the slice reducers directly to configureStore(), you never created the root reducer yourself, so you need to infer it as type RootState = ReturnType<typeof store.getState> instead.

We should update the Advanced Tutorial and Usage with TypeScript pages to mention that.

See https://stackoverflow.com/a/59827580/62937 for an example of this

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:29 (14 by maintainers)

github_iconTop GitHub Comments

20reactions
lucassuscommented, Apr 17, 2020

@msutkowski Thank you very much! You saved me quite a few hours of debugging 😉 It seems that in my case it was a problem with the reducer builder. For example instead of builder => builder.addCase(...), I should have builder => { builder.addCase(...) }.

@markerikson Why in this case these brackets are so important? Is the typing system somehow relies on the return value form the builder?

5reactions
amankkgcommented, Feb 7, 2020

Such changes would conflict with this advice from docs on getDefaultMiddleware<RootState> https://redux-starter-kit.js.org/usage/usage-with-typescript#correct-typings-for-the-dispatch-type

const store = configureStore({
  reducer: rootReducer,
  middleware: [
    ...customMiddleware,
    ...getDefaultMiddleware<RootState>(),
  ] as const,
})

// Type alias 'RootState' circularly references itself. ts(2456)
export type RootState = ReturnType<typeof store.getState>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage With TypeScript - Redux Toolkit
The easiest way of getting the State type is to define the root reducer in advance and extract its ReturnType . It is...
Read more >
Typing Redux toolkit's store in TypeScript - Stack Overflow
There's really three ways to define that: 1) hand-define it yourself; 2) Infer it from the return type of the root reducer; 3)...
Read more >
How I Setup Redux Toolkit and RTK Query the right way
Define the Root State and Dispatch Types ... We need to extract the RootState and AppDispatch from the store and export them directly...
Read more >
How to Use Redux Toolkit with TypeScript - Fullstack.io
Define the `RootState` as the return type: export type RootState = ReturnType<typeof store.getState>;. Now return to todosSlice.ts and ...
Read more >
Static Typing | React Redux
While this type could be written by hand, the easiest way to define it is to have TypeScript infer it based on what...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found