-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.d.ts
63 lines (51 loc) · 1.66 KB
/
index.d.ts
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
// Actions
export type Action<Payload> = {
type: string;
payload: Payload;
};
// Action Creator
export type ActionCreator<Input, Payload = Input> = {
(input: Input): Action<Payload>;
type: string;
};
type AsyncCallable<Input, Payload> = Input extends void
? () => Promise<Payload>
: (input: Input) => Promise<Payload>;
export type AsyncActionCreator<Input, Payload> = {
started: ActionCreator<Input, void>;
resolved: ActionCreator<Input, Payload>;
rejected: ActionCreator<Input, Error>;
} & AsyncCallable<Input, Payload>;
export type ThunkActionCreator<Input, A = any, R = any> = {
started: ActionCreator<Input, void>;
resolved: ActionCreator<Input, R>;
rejected: ActionCreator<Input, Error>;
} & AsyncCallable<Input, void>;
// Reducer helper
export type Reducer<State> = {
(state: State | undefined, action: any): State;
get: () => Reducer<State>;
case<Input, Payload>(
actionFunc: ActionCreator<Input, Payload>,
reducer: (state: State, payload: Payload) => State
): Reducer<State>;
else(fn: (s: State, a: Action<any>) => State): Reducer<State>;
};
// API
export const buildActionCreator: (
opt?: { prefix?: string }
) => {
createAction<Input, Payload>(
t?: string | void,
fn?: (input: Input) => Payload
): ActionCreator<Input, Payload>;
createAsyncAction<Input, Payload>(
t: string | void,
fn: (input: Input) => Promise<Payload>
): AsyncActionCreator<Input, Payload>;
createThunkAction<Input, A = any, S = any, R = any>(
t: string | void,
fn: (input: Input, dispatch: (a: A) => any, getState: () => S) => Promise<R>
): ThunkActionCreator<Input, A, R>;
};
export const createReducer: <T>(t: T) => Reducer<T>;