目前,我正在尝试使用以下代码创建redux存储。
import { createStore, Action } from "redux";
interface State {
page: string;
}
const reducer = (state: State, action: Action) => {
return state;
};
export const store = createStore(reducer);
但是在createStore
函数中出现错误,告诉我
No overload matches this call.
Overload 1 of 2, '(reducer: Reducer<State, Action<any>>, enhancer?: StoreEnhancer<unknown, unknown> | undefined): Store<State, Action<...>>', gave the following error.
Argument of type '(state: State, action: Action) => State' is not assignable to parameter of type 'Reducer<State, Action<any>>'.
Types of parameters 'state' and 'state' are incompatible.
Type 'State | undefined' is not assignable to type 'State'.
Type 'undefined' is not assignable to type 'State'.
Overload 2 of 2, '(reducer: Reducer<State, Action<any>>, preloadedState?: { page: string; } | undefined, enhancer?: StoreEnhancer<unknown, {}> | undefined): Store<...>', gave the following error.
Argument of type '(state: State, action: Action) => State' is not assignable to parameter of type 'Reducer<State, Action<any>>'.
还原版本为^4.1.0
。
2条答案
按热度按时间zzwlnbp81#
根据
Reducer
的定义,传入状态可以是undefined
,因此您应该如下声明reducer:db2dz4w82#
我有一个类似的问题,我解决了它的设置类型的行动在减速器功能如下:
我添加了任何操作