Skip to content

Typescript for Adding Listeners Inside Components #2798

Answered by markerikson
peterkrieg asked this question in Q&A
Discussion options

You must be logged in to vote

How are you declaring dispatch in this case?

The issue here is that by default, store.dispatch() returns exactly the action that you passed in, and the types reflect that.

However, middleware may override that return value. As an example, the thunk middleware instead returns whatever the thunk function returns (such as a promise).

RTK's listener middleware is typed so that dispatch(addListener()) should correctly return an UnsubscribeListener function. However, the basic Redux Dispatch type doesn't know that. It has to be inferred from the actual type of store.dispatch in your app setup so that the types from the middleware modify the type of the store.

If you follow our recommended patte…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@peterkrieg
Comment options

Answer selected by peterkrieg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants