UNPKG

combine-action-reducer

Version:

create action creator and reducer with one action-reduer

28 lines (27 loc) 1.13 kB
declare type ActionMapObj<S> = { [x: string]: (s: S, ac: any) => S; }; declare type getActionCreatorType<mapObj, key = unknown> = mapObj extends { [x: string]: (s: any, ac: any) => any; } ? { [ActionKey in keyof mapObj]: mapObj[ActionKey] extends (s: any, a: infer ActionPayload) => any ? (payload: ActionPayload) => { type: key & ActionKey; payload: ActionPayload; } : "Error at getActionCreatorType"; } : { [ActionPath in keyof mapObj]: getActionCreatorType<mapObj[ActionPath], ActionPath>; }; export declare type ActionReducer<S, mapObj> = { reducer: mapObj extends { [x: string]: (s: S, ac: any) => any; } ? <ActionType extends keyof mapObj>(state: S | undefined, ac: { type: ActionType; payload: mapObj[ActionType] extends (s: any, a: infer ActionPayload) => any ? ActionPayload : any; }) => S : (state: S | undefined, ac: { type: string; payload: any; }) => S; action: getActionCreatorType<mapObj>; }; export declare type ObjectReducer<S> = <MapObj extends ActionMapObj<S>>(obj: MapObj) => ActionReducer<S, MapObj>; export {};