UNPKG

8.03 kBTypeScriptView Raw
1// Type definitions for redux-actions 2.6
2// Project: https://github.com/redux-utilities/redux-actions
3// Definitions by: Jack Hsu <https://github.com/jaysoo>,
4// Alex Gorbatchev <https://github.com/alexgorbatchev>,
5// Alec Hill <https://github.com/alechill>
6// Alexey Pelykh <https://github.com/alexey-pelykh>
7// Thiago de Andrade <https://github.com/7hi4g0>
8// Ziyu <https://github.com/oddui>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10// TypeScript Version: 2.2
11
12export as namespace ReduxActions;
13
14// FSA-compliant action.
15// See: https://github.com/acdlite/flux-standard-action
16export interface BaseAction {
17 type: string;
18}
19
20export interface Action<Payload> extends BaseAction {
21 payload: Payload;
22 error?: boolean | undefined;
23}
24
25export interface ActionMeta<Payload, Meta> extends Action<Payload> {
26 meta: Meta;
27}
28
29// https://github.com/redux-utilities/redux-actions/blob/v2.3.0/src/combineActions.js#L27
30export interface CombinedActionType {
31 _dummy: undefined;
32}
33
34export type ReducerMapValue<State, Payload> = Reducer<State, Payload> | ReducerNextThrow<State, Payload> | ReducerMap<State, Payload>;
35
36export interface ReducerMap<State, Payload> {
37 [actionType: string]: ReducerMapValue<State, Payload>;
38}
39
40export interface ReducerMapMeta<State, Payload, Meta> {
41 [actionType: string]: ReducerMeta<State, Payload, Meta> | ReducerNextThrowMeta<State, Payload, Meta> | ReducerMapMeta<State, Payload, Meta>;
42}
43
44export interface ReducerNextThrow<State, Payload> {
45 next?(state: State, action: Action<Payload>): State;
46 throw?(state: State, action: Action<Payload>): State;
47}
48
49export interface ReducerNextThrowMeta<State, Payload, Meta> {
50 next?(state: State, action: ActionMeta<Payload, Meta>): State;
51 throw?(state: State, action: ActionMeta<Payload, Meta>): State;
52}
53
54export type BaseActionFunctions<TAction> =
55 ActionFunction0<TAction> |
56 ActionFunction1<any, TAction> |
57 ActionFunction2<any, any, TAction> |
58 ActionFunction3<any, any, any, TAction> |
59 ActionFunction4<any, any, any, any, TAction> |
60 ActionFunctionAny<TAction>;
61
62export type ActionFunctions<Payload> = BaseActionFunctions<Action<Payload>>;
63
64export type ActionWithMetaFunctions<Payload, Meta> = BaseActionFunctions<ActionMeta<Payload, Meta>>;
65
66export type Reducer<State, Payload> = (state: State, action: Action<Payload>) => State;
67
68export type ReducerMeta<State, Payload, Meta> = (state: State, action: ActionMeta<Payload, Meta>) => State;
69
70export type ReduxCompatibleReducer<State, Payload> = (state: State | undefined, action: Action<Payload>) => State;
71
72export type ReduxCompatibleReducerMeta<State, Payload, Meta> = (state: State | undefined, action: ActionMeta<Payload, Meta>) => State;
73
74/** argument inferring borrowed from lodash definitions */
75export type ActionFunction0<R> = () => R;
76export type ActionFunction1<T1, R> = (t1: T1) => R;
77export type ActionFunction2<T1, T2, R> = (t1: T1, t2: T2) => R;
78export type ActionFunction3<T1, T2, T3, R> = (t1: T1, t2: T2, t3: T3) => R;
79export type ActionFunction4<T1, T2, T3, T4, R> = (t1: T1, t2: T2, t3: T3, t4: T4) => R;
80export type ActionFunctionAny<R> = (...args: any[]) => R;
81
82// https://github.com/redux-utilities/redux-actions/blob/v2.3.0/src/createAction.js#L6
83export function createAction(
84 actionType: string
85): ActionFunctionAny<Action<any>>;
86
87export function createAction<Payload>(
88 actionType: string,
89 payloadCreator: ActionFunction0<Payload>
90): ActionFunction0<Action<Payload>>;
91
92export function createAction<Payload, Arg1>(
93 actionType: string,
94 payloadCreator: ActionFunction1<Arg1, Payload>
95): ActionFunction1<Arg1, Action<Payload>>;
96
97export function createAction<Payload, Arg1, Arg2>(
98 actionType: string,
99 payloadCreator: ActionFunction2<Arg1, Arg2, Payload>
100): ActionFunction2<Arg1, Arg2, Action<Payload>>;
101
102export function createAction<Payload, Arg1, Arg2, Arg3>(
103 actionType: string,
104 payloadCreator: ActionFunction3<Arg1, Arg2, Arg3, Payload>
105): ActionFunction3<Arg1, Arg2, Arg3, Action<Payload>>;
106
107export function createAction<Payload, Arg1, Arg2, Arg3, Arg4>(
108 actionType: string,
109 payloadCreator: ActionFunction4<Arg1, Arg2, Arg3, Arg4, Payload>
110): ActionFunction4<Arg1, Arg2, Arg3, Arg4, Action<Payload>>;
111
112export function createAction<Payload>(
113 actionType: string
114): ActionFunction1<Payload, Action<Payload>>;
115
116export function createAction<Meta>(
117 actionType: string,
118 payloadCreator: null | undefined,
119 metaCreator: ActionFunctionAny<Meta>
120): ActionFunctionAny<ActionMeta<any, Meta>>;
121
122export function createAction<Payload, Meta>(
123 actionType: string,
124 payloadCreator: ActionFunctionAny<Payload>,
125 metaCreator: ActionFunctionAny<Meta>
126): ActionFunctionAny<ActionMeta<Payload, Meta>>;
127
128export function createAction<Payload, Meta, Arg1>(
129 actionType: string,
130 payloadCreator: ActionFunction1<Arg1, Payload>,
131 metaCreator: ActionFunction1<Arg1, Meta>
132): ActionFunction1<Arg1, ActionMeta<Payload, Meta>>;
133
134export function createAction<Payload, Meta, Arg1, Arg2>(
135 actionType: string,
136 payloadCreator: ActionFunction2<Arg1, Arg2, Payload>,
137 metaCreator: ActionFunction2<Arg1, Arg2, Meta>
138): ActionFunction2<Arg1, Arg2, ActionMeta<Payload, Meta>>;
139
140export function createAction<Payload, Meta, Arg1, Arg2, Arg3>(
141 actionType: string,
142 payloadCreator: ActionFunction3<Arg1, Arg2, Arg3, Payload>,
143 metaCreator: ActionFunction3<Arg1, Arg2, Arg3, Meta>
144): ActionFunction3<Arg1, Arg2, Arg3, ActionMeta<Payload, Meta>>;
145
146export function createAction<Payload, Meta, Arg1, Arg2, Arg3, Arg4>(
147 actionType: string,
148 payloadCreator: ActionFunction4<Arg1, Arg2, Arg3, Arg4, Payload>,
149 metaCreator: ActionFunction4<Arg1, Arg2, Arg3, Arg4, Meta>
150): ActionFunction4<Arg1, Arg2, Arg3, Arg4, ActionMeta<Payload, Meta>>;
151
152export function handleAction<State, Payload>(
153 actionType: string | ActionFunctions<Payload> | CombinedActionType,
154 reducer: Reducer<State, Payload> | ReducerNextThrow<State, Payload>,
155 initialState: State
156): ReduxCompatibleReducer<State, Payload>;
157
158export function handleAction<State, Payload, Meta>(
159 actionType: string | ActionWithMetaFunctions<Payload, Meta> | CombinedActionType,
160 reducer: ReducerMeta<State, Payload, Meta> | ReducerNextThrowMeta<State, Payload, Meta>,
161 initialState: State
162): ReduxCompatibleReducerMeta<State, Payload, Meta>;
163
164export interface Options {
165 prefix?: string | undefined;
166 namespace?: string | undefined;
167}
168
169export function handleActions<StateAndPayload>(
170 reducerMap: ReducerMap<StateAndPayload, StateAndPayload>,
171 initialState: StateAndPayload,
172 options?: Options
173): ReduxCompatibleReducer<StateAndPayload, StateAndPayload>;
174
175export function handleActions<State, Payload>(
176 reducerMap: ReducerMap<State, Payload>,
177 initialState: State,
178 options?: Options
179): ReduxCompatibleReducer<State, Payload>;
180
181export function handleActions<State, Payload, Meta>(
182 reducerMap: ReducerMapMeta<State, Payload, Meta>,
183 initialState: State,
184 options?: Options
185): ReduxCompatibleReducerMeta<State, Payload, Meta>;
186
187// https://github.com/redux-utilities/redux-actions/blob/v2.3.0/src/combineActions.js#L21
188export function combineActions(...actionTypes: Array<ActionFunctions<any> | string | symbol>): CombinedActionType;
189
190export interface ActionMap<Payload, Meta> {
191 [actionType: string]:
192 ActionMap<Payload, Meta> |
193 ActionFunctionAny<Payload> |
194 [ActionFunctionAny<Payload>, ActionFunctionAny<Meta>] |
195 undefined;
196}
197
198export function createActions<Payload>(
199 actionMapOrIdentityAction: ActionMap<Payload, any> | string,
200 ...identityActions: Array<string | Options>
201): {
202 [actionName: string]: ActionFunctionAny<Action<Payload>>
203};
204
205export function createActions(
206 actionMapOrIdentityAction: ActionMap<any, any> | string,
207 ...identityActions: Array<string | Options>
208): {
209 [actionName: string]: ActionFunctionAny<Action<any>>
210};