Redux Thunk Typescript无法使用Async进行调度
我为redux thunk(无react,仅控制台应用程序)做了一个示例测试,代码如下:Redux Thunk Typescript无法使用Async进行调度,redux,redux-thunk,Redux,Redux Thunk,我为redux thunk(无react,仅控制台应用程序)做了一个示例测试,代码如下: interface IStartLoadAction extends Action<"START"> { } interface IEndLoadAction extends Action<"END"> { } type LoadActionTypes = IStartLoadAction | IEndLoadAction; function startLoad(): Loa
interface IStartLoadAction extends Action<"START"> {
}
interface IEndLoadAction extends Action<"END"> {
}
type LoadActionTypes = IStartLoadAction | IEndLoadAction;
function startLoad(): LoadActionTypes {
return {type: "START"};
}
function endLoad(): LoadActionTypes {
return {type: "END"};
}
const loadReducer: Reducer<ILoadState | undefined, LoadActionTypes> = (
state = initLoadState,
action: LoadActionTypes) => {
switch (action.type) {
case "START":
return {isLoading: !state.isLoading};
case "END":
return {isLoading: !state.isLoading};
default:
return {isLoading: false};
}
};
const rootReducer = combineReducers({
loader: loadReducer,
});
const store = createStore(rootReducer, applyMiddleware(Thunk));
const fetchData = (): ThunkAction<Promise<void>, ILoadState, null, AnyAction> => {
return async (dispatch: ThunkDispatch<{}, {}, AnyAction>): Promise<void> => {
return new Promise<void>((resolve) => {
dispatch(startLoad());
console.log('start....');
setTimeout(() => {
dispatch(<some actions>);
}, 2000);
dispatch(endLoad());
console.log('end....');
resolve();
});
};
};
接口IStartLoadAction扩展了操作{
}
接口IEndLoadAction扩展了操作{
}
类型LoadActionTypes=IStartLoadAction | IEndLoadAction;
函数startLoad():LoadActionTypes{
返回{type:“START”};
}
函数endLoad():LoadActionTypes{
返回{type:“END”};
}
常量减速机:减速机=(
state=initLoadState,
操作:LoadActionTypes)=>{
开关(动作类型){
案例“开始”:
返回{isLoading:!state.isLoading};
案例“结束”:
返回{isLoading:!state.isLoading};
违约:
返回{isLoading:false};
}
};
const rootReducer=combinereducer({
装载机:减速机,
});
const store=createStore(rootReducer,applyMiddleware(Thunk));
常量fetchData=():ThunkAction=>{
返回异步(分派:ThunkDispatch):Promise=>{
返回新承诺((解决)=>{
调度(startoad());
console.log('start…');
设置超时(()=>{
分派();
}, 2000);
调度(endLoad());
console.log('end…');
解决();
});
};
};
但是当我调用store.dispatch(fetchData())时代码>,它返回一个错误:
类型为'ThunkAction,ILoadState,null,
AnyAction>'不可分配给类型为“AnyAction”的参数
如何让它工作?谢谢(我不会在react env中使用它,仅限于服务器端)
Redux官方网站的示例是“js”,但不是“ts”,因此我不知道如何翻译它。我遇到了完全相同的问题-store.dispatch(actionCreator())导致错误ts-2345。网络中有一些例子-首先,它们都不起作用(相同的问题),其次,它们中的一些使用非常不透明和不可理解的类型,但从未解释原因。我有完全相同的问题存储。dispatch(actionCreator())导致错误TS-2345。网络中有一些例子——首先,它们都不起作用(同一个问题),其次,它们中的一些使用非常不透明和不可理解的类型,但从未解释过原因