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.

Add a type definition that accepts ThunkAction provided redux-thunk to useDispatch

See original GitHub issue

What is the new or updated feature that you are suggesting?

From react-redux v8, it provides own type definitions. Along with that, a difference arose from the type definitions that were provided by volunteers in TypesDefinitions.

What I am having trouble with is the type definition of useDispatch.

https://github.com/reduxjs/react-redux/blob/26ddc6a6fc1238408ae5a0cbc18f15be225786dc/src/hooks/useDispatch.ts#L25-L31

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/b153e7ef5fb60b1a20dc5120c55058fd24c2cc39/types/react-redux/index.d.ts#L538-L540

The current react-redux type definition only allows pure actions to be passed to dispatch.

Why should this feature be included?

It is helpful to maintain an ecosystem with third-party libraries surrounding react-redux.

What docs changes are needed to explain this?

Maybe nothing.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Apr 20, 2022

Hiya. The issue here is that there’s no guarantee that the actual Redux store being used was configured to have the thunk middleware. So, if we just automatically have the types include support for thunks, we’re actually lying to our users about how the runtime behavior will work.

This is also how the types in DefinitelyTyped have always behaved, as far as I know.

Because of that, we specifically teach and show how to infer the final real type from store.dispatch, and create a “pre-typed” hook that captures that for use within the rest of the app:

https://react-redux.js.org/using-react-redux/usage-with-typescript#standard-redux-toolkit-project-setup-with-typescript

0reactions
stevebeaugecommented, Apr 20, 2022

Found the difference I guess. My thunk was named UploadFilesStep while my slice was uploadFilesStep. Seems it matters somehow. thx for the help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage With TypeScript
A thunk function receives dispatch and getState as its parameters. Redux Thunk has a built in ThunkAction type which we can use to...
Read more >
typescript - type-safe useDispatch with redux-thunk
It allows to dispatch a ThunkAction , which basically is the inner function of requestUserName . For example, you can type it like...
Read more >
Example with Typescript, react-redux, and redux-thunk #213
I am getting this typescript error: Property 'then' does not exist on type 'ThunkAction<Promise<boolean>, IinitialState, undefined, any>'. FYI I ...
Read more >
How to Use Thunks with Redux Toolkit and TypeScript
All the work is done in the redux-thunk middleware. It extends the store's abilities and lets you write async logic that interacts with...
Read more >
Using TypeScript with Redux Toolkit
Simplify state management in React using Redux Toolkit and TypeScript to easily scale and adapt large projects.
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