Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configureStore() middleware action typings are wrong #289

Closed
slikts opened this issue Dec 21, 2019 · 5 comments
Closed

configureStore() middleware action typings are wrong #289

slikts opened this issue Dec 21, 2019 · 5 comments

Comments

@slikts
Copy link

slikts commented Dec 21, 2019

Currently the middleware option for configureStore() does not affect what action types store.dispatch() accepts; thunks are always accepted even if the thunk middleware is missing, and adding other middleware ignores its dispatch signature.

This is a regression from Redux, where the dispatchable action types are picked up correctly by createStore().

Example:

const store = configureStore({
  reducer: rootReducer,
  // default thunk middleware disabled
  middleware: []
});
// expected compiler error missing
store.dispatch(() => {});

I've also made a longer working example that defines a number middleware that adds a number action type and shows that the types work with plain Redux but not RTK. I've tried to work around the issue by passing explicit type parameters like so: configureStore<RootState, AnyAction | number>, but it doesn't work due to always expecting a type property on the action.

@phryneas
Copy link
Member

phryneas commented Dec 21, 2019

Fascinating, I always assumed that redux did not do that (as it is impossible in TS to type this for an arbitrary number of middlewares), but apparently the current types actually support the dispatch type of up to 5 middlewares.

Thank you for bringing that to my attention. @markerikson I guess we should add support for this as well then, shouldn't we?

In the meantime, the way to currently work around this is documented in the "Usage with TypeScript" documentation: Extending the Dispatch type

@slikts
Copy link
Author

slikts commented Dec 21, 2019

Thanks for the workaround, although it'd be slightly nicer if it were possible to use the type parameters instead.

@markerikson
Copy link
Collaborator

Yeah, definitely be nice if we can fix this.

@phryneas
Copy link
Member

@slikts I just finished my work on the PR, so this will most likely make it into the next version of RTK.
If you want to already use it, you can use the codesandbox build for the PR. Early feedback is highly welcome :)

@markerikson
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants