Typescript:如何键入处理异步操作的自定义 redux 中间件
Typescript: how to type a custom redux middleware that handles async actions
我希望这个周末每个人都过得很好!
免责声明:我已经研究了一整天,我尝试了很多东西之后才来寻求帮助,我打开了 20 多个 Whosebug links 并通读了它们...
我有这个自定义的 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);
};
我要解决的问题是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>
这是因为 incrementAsync
使用来自“@reduxjs/toolkit”的 createAsyncThunk
;
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;
}
);
所以我想我需要把最后一个函数 ``async (action: AnyAction) => {` 类型的动作改成别的,但我想不通,请问各位大神指教单词?
这里有一个 link typescript playground 如果有帮助的话
试试
const errorMiddleware: Middleware<{}, unknown, ThunkDispatch<unknown, unknown, AnyAction>> =
({ dispatch }) =>
(next) =>
async (action) => {
无需在 =
左侧 和 右侧输入内容 - 在一侧输入会转移到另一侧。
我希望这个周末每个人都过得很好!
免责声明:我已经研究了一整天,我尝试了很多东西之后才来寻求帮助,我打开了 20 多个 Whosebug links 并通读了它们...
我有这个自定义的 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);
};
我要解决的问题是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>
这是因为 incrementAsync
使用来自“@reduxjs/toolkit”的 createAsyncThunk
;
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;
}
);
所以我想我需要把最后一个函数 ``async (action: AnyAction) => {` 类型的动作改成别的,但我想不通,请问各位大神指教单词?
这里有一个 link typescript playground 如果有帮助的话
试试
const errorMiddleware: Middleware<{}, unknown, ThunkDispatch<unknown, unknown, AnyAction>> =
({ dispatch }) =>
(next) =>
async (action) => {
无需在 =
左侧 和 右侧输入内容 - 在一侧输入会转移到另一侧。