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