Created
May 21, 2018 05:29
-
-
Save lukaszx0/23b44584e3587e91406078d89be9bd5a to your computer and use it in GitHub Desktop.
Typescript Redux Action helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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