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.

Default `EnhancedStore.dispatch` unusable in TS in 1.8.0?

See original GitHub issue

1.8.0’s changes to middleware dispatch types apparently mean that the default EnhancedStore.dispatch is typed as never in TypeScript.

import { CombinedState, configureStore, EnhancedStore } from '@reduxjs/toolkit';

let store: EnhancedStore<CombinedState<{ name: string }>> | undefined;
store = configureStore({ reducer: (state) => state || { name: 'Test'} });
store.dispatch({});

In RTK 1.8.0, this results in the following TypeScript error:

This expression is not callable.
  Type 'never' has no call signatures. ts(2349)

In RTK 1.7.2, it compiles without errors.

I can work around this by specifying my own middleware type argument to EnhancedStore or by inferring the type with typeof, but that feels much more awkward.

Thank you.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
markeriksoncommented, Mar 7, 2022

Okay, the issue here is the use of a separate ConfigureStoreOptions. You’ve erased any actual types that could be inferred.

Please don’t write that separately. If you do everything inline in configureStore, the types are correct:

export const store = configureStore({
  reducer: {
    [feedbackSlice.name]: feedbackSlice.reducer
  },
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat()
});

image

It looks like the main reason you’re doing that is to make the devTools option configurable. I’d suggest just having a separate let devTools boolean and conditionally assign to that, then pass it as an argument to configureStore.

1reaction
phryneascommented, Mar 7, 2022

Generally, when using RTK you should never declare an explicit type for a variable. That is erasing all type information until there is only the info “Yeah, this has the shape of a configuration. Maybe there are reducers in there. Maybe middleware.” is left.

Read more comments on GitHub >

github_iconTop Results From Across the Web

this expression is not callable. type 'never' has no call ...
1.8.0's changes to middleware dispatch types apparently mean that the default EnhancedStore.dispatch is typed as never in TypeScript.
Read more >
ReactJS Redux Provider won't take the store - Stack Overflow
The default middleware is, as the name implies, the default. Also, please note that the deprecation of createStor is just an indicator that ......
Read more >
Usage With TypeScript - Redux Toolkit
By default, the React Redux useDispatch hook does not contain any types that take middlewares into account. If you need a more specific...
Read more >
Using TypeScript with Redux Toolkit - LogRocket Blog
Configuring the store with configureStore · // src/app/rootReducer.ts · import · from · '@reduxjs/toolkit' · const · export · export · default ......
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