Attempting to streamline my action shorthand that interacts with AsyncActionCreators
.
A function has been crafted to accept a React dispatch: Dispatch<T>
parameter:
const fetchProfileAction = actionCreator.async<void, Profile, any>('FETCH_PROFILE');
// AsyncActionCreators<Params = void, Result = Profile, Error = any>;
type AsyncDispatch<A extends AsyncActionCreators<Params, Result, Error>, Params, Result, Error> = Dispatch<
ReturnType<A['started'] | A['done'] | A['failed']>
>;
export const fetchProfile = (
dispatch: AsyncDispatch<typeof fetchProfileAction, void, Profile, any>,
) => async () => {
dispatch(fetchProfileAction.started());
try {
dispatch(fetchProfileAction.done({ result: (await api.get<Profile>('/profile')).data }));
} catch (e) {
dispatch(fetchProfileAction.failed(e));
}
};
The current challenge lies in having to manually specify the types within fetchProfileAction
:
AsyncDispatch<typeof fetchProfileAction, void, Profile, any>
Is there a way to simplify this and achieve something like
AsyncDispatch<typeof fetchProfileAction>
, automatically resolving Params
, Result
, Error
types of AsyncActionCreators
?