Dmitry Samoylov
Dmitry Samoylov

Reputation: 1318

How to createStore with composeWithDevTools in Typescript without type errors?

In my index.tsx I create redux store

import { createStore, Store } from 'redux';
...
const store: Store = createStore(reducers, {});

Now I try to add composeWithDevTools

import { createStore, Store, compose } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
...
const store: Store = createStore(reducers, {}, compose(composeWithDevTools()));

and what I get is the error

Type 'Store<CombinedState<{ general: any; }>, StoreAction>' is not assignable to type 'Store<any, AnyAction>'.
  Types of property 'dispatch' are incompatible.
    Type 'Dispatch<StoreAction>' is not assignable to type 'Dispatch<AnyAction>'.
      Type 'AnyAction' is not assignable to type 'StoreAction'.  TS2322

Looks like it is somehow related with my StoreAction type. In my reducers I have following

export default (state: GeneralState = initialState, action: StoreAction) => {
...
};

and StoreAction is

type StoreAction = {
  type: string;
  payload: any;
};

And this StoreAction type is incompatible with AnyAction interface. I don't understand how can I fix it correctly from TypeScript perspective.

Any ideas how can I fix the error?

Upvotes: 2

Views: 3119

Answers (1)

Paul
Paul

Reputation: 1022

It looks like you don't need to wrap composeWithDevTools in a compose statement.

From the docs:

  const composedEnhancers = composeWithDevTools(...enhancers)
  const store = createStore(rootReducer, preloadedState, composedEnhancers)

https://redux.js.org/recipes/configuring-your-store/

Upvotes: 3

Related Questions