简体   繁体   中英

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM