forked from flow-typed/flow-typed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redux-actions_v2.x.x.js
107 lines (95 loc) · 3.59 KB
/
redux-actions_v2.x.x.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
declare module "redux-actions" {
/*
* Use `ActionType` to get the type of the action created by a given action
* creator. For example:
*
* import { createAction, type ActionType } from 'redux-actions'
*
* const increment = createAction(INCREMENT, (count: number) => count)
*
* function myReducer(state: State = initState, action: ActionType<typeof increment>): State {
* // Flow will infer that the type of `action.payload` is `number`
* }
*/
declare type ActionType<ActionCreator> = _ActionType<*, ActionCreator>;
declare type _ActionType<R, Fn: (payload: *, ...rest: any[]) => R> = R;
/*
* To get the most from Flow type checking use a `payloadCreator` argument
* with `createAction`. Make sure that Flow can infer the argument type of the
* `payloadCreator`. That will allow Flow to infer the payload type of actions
* created by that action creator in other parts of the program. For example:
*
* const increment = createAction(INCREMENT, (count: number) => count)
*
*/
declare function createAction<T, P>(
type: T,
$?: empty // hack to force Flow to not use this signature when more than one argument is given
): (payload: P, ...rest: any[]) => { type: T, payload: P, error?: boolean };
declare function createAction<T, P, P2>(
type: T,
payloadCreator: (_: P) => P2,
$?: empty
): (payload: P, ...rest: any[]) => { type: T, payload: P2, error?: boolean };
declare function createAction<T, P, P2, M>(
type: T,
payloadCreator: (_: P) => P2,
metaCreator: (_: P) => M
): (
payload: P,
...rest: any[]
) => { type: T, payload: P2, error?: boolean, meta: M };
declare function createAction<T, P, M>(
type: T,
payloadCreator: null | void,
metaCreator: (_: P) => M
): (
payload: P,
...rest: any[]
) => { type: T, payload: P, error?: boolean, meta: M };
// `createActions` is quite difficult to write a type for. Maybe try not to
// use this one?
declare function createActions(
actionMap: Object,
...identityActions: string[]
): Object;
declare function createActions(...identityActions: string[]): Object;
declare type Reducer<S, A> = (state: S, action: A) => S;
declare type ReducerMap<S, A> =
| { next: Reducer<S, A> }
| { throw: Reducer<S, A> }
| { next: Reducer<S, A>, throw: Reducer<S, A> };
/*
* To get full advantage from Flow, use a type annotation on the action
* argument to your reducer when creating a reducer with `handleAction` or
* `handleActions`. For example:
*
* import { type Reducer } from 'redux'
* import { createAction, handleAction, type Action } from 'redux-actions'
*
* const increment = createAction(INCREMENT, (count: number) => count)
*
* const reducer = handleAction(INCREMENT, (state, { payload }: ActionType<typeof increment>) => {
* // Flow infers that the type of `payload` is number
* }, defaultState)
*/
declare type ReducerDefinition<State, Action> = {
[key: string]:
| (Reducer<State, Action> | ReducerDefinition<State, Action>)
| ReducerMap<State, Action>
};
declare function handleAction<Type, State, Action: { type: Type }>(
type: Type,
reducer: ReducerDefinition<State, Action>,
defaultState: State
): Reducer<State, Action>;
declare function handleActions<State, Action>(
reducers: {
[key: string]: Reducer<State, Action> | ReducerMap<State, Action>
},
defaultState?: State
): Reducer<State, Action>;
declare function combineActions(
...types: (string | Symbol | Function)[]
): string;
}