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.

`configureStore()` middleware action typings are wrong

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
phryneascommented, Jan 10, 2020

@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 😃

1reaction
phryneascommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typing dispatch on a redux store correctly when using multiple ...
Adding on to this, I was able to add the redux-logger via return getDefaultMiddleware().concat(logger) in the middleware callback, but the ...
Read more >
Usage With TypeScript - Redux
Type safety for reducers, state and action creators, and UI components ... Using configureStore should not need any additional typings.
Read more >
How to get better and easier state management with Redux ...
Learn through a simple project what state management improvements Redux Toolkit has to offer and whether it will be ideal for your project....
Read more >
Top 5 @reduxjs/toolkit Code Examples - Snyk
actions /action-types' import { createReducer } from '@reduxjs/toolkit' import ... import { configureStore, getDefaultMiddleware } from "@reduxjs/toolkit"; ...
Read more >
Setting Up Redux Toolkit and Dispatching an Asynchronous ...
Dispatching an asynchronous action to the reducer ... “It returns an array containing the default middleware installed by ConfigureStore().
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