【发布时间】:2021-07-11 15:00:47
【问题描述】:
我正在努力为我的 AsyncThunkAction 的返回类型找到正确的类型,以便将它与 Redux Toolkit 的 unwrapResult 方法一起使用(请参阅:Redux Tookit: Unwrapping Result Actions):
在 Slice 中声明异步 thunk:
export
const createChannel = createAsyncThunk<
{ id: string },
ChannelReqDto,
InjectedAxiosClientType
>("channel/createChannel", async (req, thunkApi) => {
const result = await thunkApi.extra.client.post(
`/store/${req.storeId}/channels`,
{
category: req.category,
group: req.group,
name: req.name,
rules: [],
}
);
return result.data;
});
在 React 组件中的使用:
const newChannel = await dispatch(
createChannel({
name: name,
group: group,
category: category,
storeId: storeIdParam,
rules: [],
})
);
const result = unwrapResult(newChannel);
TypeScript 错误:
TS2345: Argument of type 'AsyncThunkAction<{ id: string; }, ChannelReqDto, InjectedAxiosClientType>' is not assignable to parameter of type 'UnwrappableAction'. Property 'payload' is missing in type 'AsyncThunkAction<{ id: string; }, ChannelReqDto, InjectedAxiosClientType>' but required in type 'UnwrappableAction'.
结果类型需要扩展接口UnwrappableAction,但不知怎的,如果没有 ts-ignore,我无法让它工作:
export declare function unwrapResult<R extends UnwrappableAction>(action: R): UnwrappedActionPayload<R>;
【问题讨论】:
标签: typescript redux redux-toolkit