【发布时间】:2020-02-19 13:05:59
【问题描述】:
我尝试使用带有 redux-thunk 和 redux 的 typescript。 我想将一个对象传递给任何操作(我正在使用依赖注入,我需要传递一个对象服务或字符串)
我收到了这个错误;
Overload 1 of 2, '(action: AnyAction): AnyAction', gave the following error.
Argument of type '(dispatch: any, getState: AppState, extraArg: string) => Promise<any>' is not assignable to parameter of type 'AnyAction'.
如下:
store.ts:
const middlewares = [
routerMiddleware(history),
thunkMiddleware.withExtraArgument('bar') as ThunkMiddleware<
AppState,
Actions,
string
>
];
const middleWareEnhancer = applyMiddleware(...middlewares);
const store = createStore(
rootReducer(history),
preloadState,
composeWithDevTools(middleWareEnhancer)
);
我试图发送操作的 index.ts 是:
(store.dispatch as ThunkDispatch<{}, void, AnyAction>)(getAll());
我的动作文件有效:
export const getall = () => async (dispatch: any) => {
try {
dispatch({ type: GETALL_REQUEST });
return dispatch({ type: GETALL_SUCCESS, payload: null });
} catch (e) {
return dispatch({ type: GETALL_FAILURE, error: e });
}
};
引发错误的我的操作(放入其中的一部分)
export const getAll = () => async (
dispatch: any,
getState: AppState,
extraArg: string
) => {....action logic}
然后我得到了错误:
(store.dispatch as ThunkDispatch<{}, void, AnyAction>)(getAll());
【问题讨论】:
标签: reactjs typescript redux redux-thunk