Skip to content

Instantly share code, notes, and snippets.

@Dominent
Last active November 22, 2019 10:47
Show Gist options
  • Save Dominent/1049e5e6a848ad9eb9cc07d87bb27cc2 to your computer and use it in GitHub Desktop.
Save Dominent/1049e5e6a848ad9eb9cc07d87bb27cc2 to your computer and use it in GitHub Desktop.
interface IPayload<T> {
payload: T;
}
class Payload {
public static empty: IPayload<{}> = { payload: null } as IPayload<{}>;
}
interface IHttpTypedAction<R, S, F> {
request: ActionCreator<string, (props: IPayload<R>) => IPayload<R> & TypedAction<string>>;
success: ActionCreator<string, (props: IPayload<S>) => IPayload<S> & TypedAction<string>>;
failure: ActionCreator<string, (props: IPayload<F>) => IPayload<F> & TypedAction<string>>;
}
function createHttpAction<R = {}, S = {}, F = {}>(
type: string
): IHttpTypedAction<R, S, F> {
return {
request: createAction<string, IPayload<R>>(`REQUEST_${type}`, props<IPayload<R>>()),
success: createAction<string, IPayload<S>>(`SUCCESS_${type}`, props<IPayload<S>>()),
failure: createAction<string, IPayload<F>>(`FAILURE_${type}`, props<IPayload<F>>())
};
}
export const fetchSomethingAction = createHttpAction<number, IType>('FETCH_SOMETHING');
fetchSomethingAction.request({ payload: 1232 });
fetchSomethingAction.success({ payload: {} as ISOMETHING });
fetchSomethingAction.failure(Payload.empty);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment