Wag*_*ira 5 middleware typescript redux-middleware redux-toolkit
我希望每个人这个周末都过得很好!
免责声明:我已经研究了一整天,在尝试了很多事情之后,我打开了 20 多个 StackOverflow 链接并阅读了它们,然后才来寻求帮助......
我有这个自定义的 redux 中间件,它的目的是捕获失败的承诺并为每个承诺分派一个操作。
const errorMiddleware: Middleware =
({ dispatch }: MiddlewareAPI) =>
(next: Dispatch) =>
async (action: AnyAction) => {
const { meta, payload } = action;
const { withErrorHandler, defaultErrorCode } = meta;
const isPromise = getIsPromise(payload);
if (isPromise && withErrorHandler) {
return next(action)
.catch(errorHandler(defaultErrorCode))
.catch((handledError: ISystemError) =>
Promise.reject(
handledError.shouldSkipHandleError
? handledError
: dispatch(addError(handledError))
)
);
}
return next(action);
};
Run Code Online (Sandbox Code Playgroud)
我想解决的问题是async (action: AnyAction) => {
我收到一条错误消息说
Argument of type 'AsyncThunkAction<number, number, {}>' is not assignable to parameter of type 'AnyAction'.
Property 'type' is missing in type 'AsyncThunkAction<number, number, {}>' but required in type 'AnyAction'. TS2345
53 | <button
54 | className={styles.asyncButton}
> 55 | onClick={() => dispatch(incrementAsync(incrementValue))}
| ^
56 | >
57 | Add Async
58 | </button>
Run Code Online (Sandbox Code Playgroud)
这是因为incrementAsync使用了createAsyncThunk“@reduxjs/toolkit”;
export const incrementAsync = createAsyncThunk(
'counter/fetchCount',
async (amount: number) => {
const response = await fetchCount(amount);
// The value we return becomes the `fulfilled` action payload
return response.data;
}
);
Run Code Online (Sandbox Code Playgroud)
所以我相信我需要将最后一个 function ``async (action: AnyAction) => {` 类型的操作更改为其他类型,但我无法弄清楚,我可以得到你的话的启发吗?
如果有帮助的话,这是打字稿游乐场的链接
尝试一下
const errorMiddleware: Middleware<{}, unknown, ThunkDispatch<unknown, unknown, AnyAction>> =
({ dispatch }) =>
(next) =>
async (action) => {
Run Code Online (Sandbox Code Playgroud)
无需在左右两侧键入内容-在=一侧键入内容将转移到另一侧。
| 归档时间: |
|
| 查看次数: |
1462 次 |
| 最近记录: |