reactjs - 如何在 Typescript 中使用 composeWithDevTools createStore 而不会出现类型错误?

标签 reactjs typescript redux

在我的index.tsx中我创建了redux存储

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

现在我尝试添加 composeWithDevTools

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

我得到的是错误

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

看起来它与我的 StoreAction 类型有某种关系。 在我的 reducer 中,我有以下内容

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

StoreAction 是

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

并且此 StoreAction 类型 与 AnyAction 接口(interface) 不兼容。我不明白如何从 TypeScript 的角度正确修复它。

有什么想法可以解决这个错误吗?

最佳答案

看起来您不需要将 composeWithDevTools 包装在 compose 语句中。

来自文档:

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

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

关于reactjs - 如何在 Typescript 中使用 composeWithDevTools createStore 而不会出现类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59967720/

相关文章:

reactjs - 从无状态组件返回 null/"functional component"

javascript - React 组件 this.variable 不渲染

reactjs - react : Don't render components in table who aren't visible

javascript - 在 Vue 项目中使 axios 全局化不起作用

typescript - 使用 Webpack 根据模式要求不同的应用程序配置文件

reactjs - 在reactjs构造函数redux中设置初始状态的最佳实践是什么?

javascript - 当任何状态发生变化时,在 React 组件的渲染函数中调用返回 JSX 的方法是否会影响性能?

typescript - Angular 2 - 在输入变量更改时更改邻居输入变量

html - Redux 是存储 JWT token 的安全场所吗?

javascript - React/Redux - 尽管 Redux 状态发生变化,组件仍不更新