Skip to content

Instantly share code, notes, and snippets.

@lukaszx0
Created May 21, 2018 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaszx0/23b44584e3587e91406078d89be9bd5a to your computer and use it in GitHub Desktop.
Save lukaszx0/23b44584e3587e91406078d89be9bd5a to your computer and use it in GitHub Desktop.
Typescript Redux Action helpers
export interface Action<T extends string> {
type: T;
}
export interface ActionWithPayload<T extends string, P> extends Action<T> {
payload: P;
}
export function createAction<T extends string>(type: T): Action<T>;
export function createAction<T extends string, P>(type: T, payload: P): ActionWithPayload<T, P>;
export function createAction<T extends string, P>(type: T, payload?: P) {
return payload === undefined ? { type } : { type, payload };
}
type FunctionType = (...args: any[]) => any;
type ActionCreatorsMapObject = { [actionCreator: string]: FunctionType };
export type ActionsUnion<A extends ActionCreatorsMapObject> = ReturnType<A[keyof A]>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment