我在解决如何传递通过CreateAsyncAction创建的AsyncAction同时保留输入信息方面遇到了一些麻烦,例如在我的情况下,我试图使用ApolloClient创建一个通用的GraphQL查询史诗,因为所有这些将具有相同的REQUEST / SUCCESS / FAILURE流程:
asyncAction应该是什么类型才能使isActionOf和action.Payload正确解析?
const createAsyncEpic = (asyncAction: <type?>, mutation: any) {
const epic: Epic<RootAction, RootState> = (action$, state$) =>
action$.pipe(
filter(isActionOf(actions.request)),
withLatestFrom(state$),
mergeMap(([action, state]) =>
client
.mutate({ mutation, variables: { ...action.payload } })
.then((result: ApolloQueryResult<P2>) => {
return actions.success(result.data);
})
.catch((error: ApolloError) => {
return actions.failure(error);
})
)
);
return epic;
};
谢谢!
答案 0 :(得分:0)
在typesafe-actions@4.3.0
中,我们添加了对使用AsyncActionCreator
类型的异步操作创建显式类型的支持。
以下是文档:https://github.com/piotrwitek/typesafe-actions#asyncactioncreator
在您的情况下,您可以尝试使用类似的方法从传递的异步操作arg中推断类型:
const createAsyncEpic = <TRequestType extends string, TRequestPayload, TSuccessType extends string, TSuccessPayload, TFailureType extends string, TFailurePayload>(
asyncAction: AsyncActionCreator<
[TRequestType, TRequestPayload],
[TSuccessType, TSuccessPayload],
[TFailureType, TFailurePayload],
>,
mutation: any
) {
...