UNPKG

7.66 kBTypeScriptView Raw
1/**
2 * PUBLIC API
3 */
4/**
5 * @desc Interface for internal types augmentation
6 * @example ```
7 * declare module 'typesafe-actions' {
8 * export type RootAction = ActionType<typeof import('./root-action').default>;
9 * export interface Types {
10 * RootAction: RootAction;
11 * }
12 * } ```
13 */
14export interface Types {
15}
16/**
17 * @desc Type representing Type Constant
18 */
19export declare type TypeConstant = string;
20/**
21 * @desc Type representing Generic Action
22 */
23export declare type Action<TType extends TypeConstant = TypeConstant> = {
24 type: TType;
25};
26/**
27 * @desc Type representing Generic ActionCreator
28 */
29export declare type ActionCreator<TType extends TypeConstant = TypeConstant> = (...args: any[]) => Action<TType>;
30/**
31 * @desc Type representing Generic Reducer
32 */
33export declare type Reducer<TState, TAction extends Action> = (state: TState | undefined, action: TAction) => TState;
34/**
35 * @desc Action without Payload
36 */
37export declare type EmptyAction<TType extends TypeConstant> = {
38 type: TType;
39};
40/**
41 * @desc Action with only Payload
42 */
43export declare type PayloadAction<TType extends TypeConstant, TPayload> = {
44 type: TType;
45 payload: TPayload;
46};
47/**
48 * @desc Action with both Payload and Meta
49 */
50export declare type PayloadMetaAction<TType extends TypeConstant, TPayload, TMeta> = {
51 type: TType;
52 payload: TPayload;
53 meta: TMeta;
54};
55/**
56 * @desc Action Creator producing EmptyAction
57 */
58export declare type EmptyActionCreator<TType extends TypeConstant> = () => EmptyAction<TType>;
59/**
60 * @desc Action Creator producing PayloadAction
61 */
62export declare type PayloadActionCreator<TType extends TypeConstant, TPayload> = (payload: TPayload) => PayloadAction<TType, TPayload>;
63/**
64 * @desc Action Creator producing PayloadMetaAction
65 */
66export declare type PayloadMetaActionCreator<TType extends TypeConstant, TPayload, TMeta> = (payload: TPayload, meta: TMeta) => PayloadMetaAction<TType, TPayload, TMeta>;
67/**
68 * @desc Type representing type getter on Action Creator instance
69 */
70export interface ActionCreatorTypeMetadata<TType extends TypeConstant> {
71 getType?: () => TType;
72}
73/**
74 * @desc Infers Action union-type from action-creator map object
75 */
76export declare type ActionType<TActionCreatorOrMap extends any> = TActionCreatorOrMap extends ActionCreator<TypeConstant> ? ReturnType<TActionCreatorOrMap> : TActionCreatorOrMap extends Record<any, any> ? {
77 [K in keyof TActionCreatorOrMap]: ActionType<TActionCreatorOrMap[K]>;
78}[keyof TActionCreatorOrMap] : TActionCreatorOrMap extends infer R ? never : never;
79/**
80 * @desc Infers State object from reducer map object
81 */
82export declare type StateType<TReducerOrMap extends any> = TReducerOrMap extends Reducer<any, any> ? ReturnType<TReducerOrMap> : TReducerOrMap extends Record<any, any> ? {
83 [K in keyof TReducerOrMap]: StateType<TReducerOrMap[K]>;
84} : never;
85/**
86 * @desc todo
87 */
88export declare type ActionBuilder<TType extends TypeConstant, TPayload extends any = undefined, TMeta extends any = undefined> = [TMeta] extends [undefined] ? [TPayload] extends [undefined] ? unknown extends TPayload ? PayloadAction<TType, TPayload> : unknown extends TMeta ? PayloadMetaAction<TType, TPayload, TMeta> : EmptyAction<TType> : PayloadAction<TType, TPayload> : PayloadMetaAction<TType, TPayload, TMeta>;
89/**
90 * @desc todo
91 */
92export declare type ActionCreatorBuilder<TType extends TypeConstant, TPayload extends any = undefined, TMeta extends any = undefined> = [TMeta] extends [undefined] ? [TPayload] extends [undefined] ? unknown extends TPayload ? PayloadActionCreator<TType, TPayload> : unknown extends TMeta ? PayloadMetaActionCreator<TType, TPayload, TMeta> : EmptyActionCreator<TType> : PayloadActionCreator<TType, TPayload> : PayloadMetaActionCreator<TType, TPayload, TMeta>;
93/**
94 * @desc todo
95 */
96export declare type AsyncActionCreatorBuilder<TRequest extends [TType1, TPayload1] | [TType1, [TPayload1, TMeta1]] | [TType1, TArgs1, TPayload1] | [TType1, TArgs1, [TPayload1, TMeta1]], TSuccess extends [TType2, TPayload2] | [TType2, [TPayload2, TMeta2]] | [TType2, TArgs2, TPayload2] | [TType2, TArgs2, [TPayload2, TMeta2]], TFailure extends [TType3, TPayload3] | [TType3, [TPayload3, TMeta3]] | [TType3, TArgs3, TPayload3] | [TType3, TArgs3, [TPayload3, TMeta3]], TCancel extends [TType4, TPayload4] | [TType4, [TPayload4, TMeta4]] | [TType4, TArgs4, TPayload4] | [TType4, TArgs4, [TPayload4, TMeta4]] = never, TType1 extends TypeConstant = TRequest[0], TPayload1 = TRequest extends [TType1, any, [any, any]] ? TRequest[2][0] : TRequest extends [TType1, any, any] ? TRequest[2] : TRequest extends [TType1, [any, any]] ? TRequest[1][0] : TRequest[1], TMeta1 = TRequest extends [TType1, any, [any, any]] ? TRequest[2][1] : TRequest extends [TType1, [any, any]] ? TRequest[1][1] : never, TArgs1 extends any[] = TRequest extends [TType1, any, any] ? TRequest[1] : never, TType2 extends TypeConstant = TSuccess[0], TPayload2 = TSuccess extends [TType2, any, [any, any]] ? TSuccess[2][0] : TSuccess extends [TType2, any, any] ? TSuccess[2] : TSuccess extends [TType2, [any, any]] ? TSuccess[1][0] : TSuccess[1], TMeta2 = TSuccess extends [TType2, any, [any, any]] ? TSuccess[2][1] : TSuccess extends [TType2, [any, any]] ? TSuccess[1][1] : never, TArgs2 extends any[] = TSuccess extends [TType2, any, any] ? TSuccess[1] : never, TType3 extends TypeConstant = TFailure[0], TPayload3 = TFailure extends [TType3, any, [any, any]] ? TFailure[2][0] : TFailure extends [TType3, any, any] ? TFailure[2] : TFailure extends [TType3, [any, any]] ? TFailure[1][0] : TFailure[1], TMeta3 = TFailure extends [TType3, any, [any, any]] ? TFailure[2][1] : TFailure extends [TType3, [any, any]] ? TFailure[1][1] : never, TArgs3 extends any[] = TFailure extends [TType3, any, any] ? TFailure[1] : never, TType4 extends TypeConstant = TCancel[0], TPayload4 = TCancel extends [TType4, any, [any, any]] ? TCancel[2][0] : TCancel extends [TType4, any, any] ? TCancel[2] : TCancel extends [TType4, [any, any]] ? TCancel[1][0] : TCancel[1], TMeta4 = TCancel extends [TType4, any, [any, any]] ? TCancel[2][1] : TCancel extends [TType4, [any, any]] ? TCancel[1][1] : never, TArgs4 extends any[] = TCancel extends [TType4, any, any] ? TCancel[1] : never> = [TCancel] extends [never] ? {
97 request: [TArgs1] extends [never] ? ActionCreatorBuilder<TType1, TPayload1, TMeta1> : (...args: TArgs1) => ActionBuilder<TType1, TPayload1, TMeta1>;
98 success: [TArgs2] extends [never] ? ActionCreatorBuilder<TType2, TPayload2, TMeta2> : (...args: TArgs2) => ActionBuilder<TType2, TPayload2, TMeta2>;
99 failure: [TArgs3] extends [never] ? ActionCreatorBuilder<TType3, TPayload3, TMeta3> : (...args: TArgs3) => ActionBuilder<TType3, TPayload3, TMeta3>;
100} : {
101 request: [TArgs1] extends [never] ? ActionCreatorBuilder<TType1, TPayload1, TMeta1> : (...args: TArgs1) => ActionBuilder<TType1, TPayload1, TMeta1>;
102 success: [TArgs2] extends [never] ? ActionCreatorBuilder<TType2, TPayload2, TMeta2> : (...args: TArgs2) => ActionBuilder<TType2, TPayload2, TMeta2>;
103 failure: [TArgs3] extends [never] ? ActionCreatorBuilder<TType3, TPayload3, TMeta3> : (...args: TArgs3) => ActionBuilder<TType3, TPayload3, TMeta3>;
104 cancel: [TArgs4] extends [never] ? ActionCreatorBuilder<TType4, TPayload4, TMeta4> : (...args: TArgs4) => ActionBuilder<TType4, TPayload4, TMeta4>;
105};
106/**
107 * INTERNAL API
108 */
109/** @private */
110export declare type ResolveType<T extends any> = T extends (...args: any[]) => {} ? T : {
111 [K in keyof T]: T[K];
112};
113/** @private */
114export declare type ExcludeNever<T extends object> = Pick<T, {
115 [Key in keyof T]: T[Key] extends never ? never : Key;
116}[keyof T]>;