|
3 | 3 | // K - Store state
|
4 | 4 | // I - Injected props to wrapped component
|
5 | 5 |
|
6 |
| -export type Listener<K> = (state: K, action?: Action<K>, update?: Partial<K>) => void; |
| 6 | +export type Listener<K, E> = (state: K, action?: Action<K, E>, update?: Partial<K>) => void; |
7 | 7 | export type Unsubscribe = () => void;
|
8 | 8 |
|
9 |
| -export type AsyncActionFn<K> = (getState: () => K, action: (action: Action<K>) => Promise<void> | void) => Promise<Partial<K> | void>; |
10 |
| -export type SyncActionFn<K> = (getState: () => K, action: (action: Action<K>) => Promise<void> | void) => Partial<K> | void; |
11 |
| -export type ActionFn<K> = AsyncActionFn<K> | SyncActionFn<K>; |
| 9 | +export type AsyncActionFn<K, E> = (getState: () => K, action: (action: Action<K, E>) => Promise<void> | void, extraArg: E) => Promise<Partial<K> | void>; |
| 10 | +export type SyncActionFn<K, E> = (getState: () => K, action: (action: Action<K, E>, E) => Promise<void> | void, extraArg: E) => Partial<K> | void; |
| 11 | +export type ActionFn<K, E> = AsyncActionFn<K, E> | SyncActionFn<K, E>; |
12 | 12 |
|
13 |
| -export type AsyncActionObject<K> = { |
| 13 | +export type AsyncActionObject<K, E> = { |
14 | 14 | type: string;
|
15 |
| - action: AsyncActionFn<K>; |
| 15 | + action: AsyncActionFn<K, E>; |
16 | 16 | }
|
17 |
| -export type SyncActionObject<K> = { |
| 17 | +export type SyncActionObject<K, E> = { |
18 | 18 | type: string;
|
19 |
| - action: SyncActionFn<K>; |
| 19 | + action: SyncActionFn<K, E>; |
20 | 20 | }
|
21 |
| -export type ActionObject<K> = AsyncActionObject<K> | SyncActionObject<K>; |
| 21 | +export type ActionObject<K, E> = AsyncActionObject<K, E> | SyncActionObject<K, E>; |
22 | 22 |
|
23 |
| -export type Action<K> = ActionObject<K> | ActionFn<K>; |
| 23 | +export type Action<K, E> = ActionObject<K, E> | ActionFn<K, E>; |
24 | 24 |
|
25 |
| -export type AsyncActionCreator<K> = (...args: any[]) => AsyncActionFn<K> | AsyncActionObject<K>; |
26 |
| -export type SyncActionCreator<K> = (...args: any[]) => SyncActionFn<K> | SyncActionObject<K>; |
27 |
| -export type ActionCreator<K> = AsyncActionCreator<K> | SyncActionCreator<K>; |
| 25 | +export type AsyncActionCreator<K, E> = (...args: any[]) => AsyncActionFn<K, E> | AsyncActionObject<K, E>; |
| 26 | +export type SyncActionCreator<K, E> = (...args: any[]) => SyncActionFn<K, E> | SyncActionObject<K, E>; |
| 27 | +export type ActionCreator<K, E> = AsyncActionCreator<K, E> | SyncActionCreator<K, E>; |
28 | 28 |
|
29 |
| -export type ActionCreatorsObject<K> = { |
30 |
| - [actionCreator: string]: ActionCreator<K> |
| 29 | +export type ActionCreatorsObject<K, E> = { |
| 30 | + [actionCreator: string]: ActionCreator<K, E> |
31 | 31 | }
|
32 | 32 |
|
33 | 33 | export type MappedActionCreators<A> = {
|
34 |
| - [P in keyof A]: A[P] extends AsyncActionCreator<any> ? (...args: any[]) => Promise<void> : (...args: any[]) => void |
| 34 | + [P in keyof A]: A[P] extends AsyncActionCreator<any, any> ? (...args: any[]) => Promise<void> : (...args: any[]) => void |
35 | 35 | }
|
36 | 36 |
|
37 | 37 |
|
38 |
| -export interface Store<K> { |
39 |
| - action(action: Action<K>): Promise<void> | void; |
40 |
| - setState<U extends keyof K>(update: Pick<K, U>, overwrite?: boolean, action?: Action<K>): void; |
41 |
| - subscribe(f: Listener<K>): Unsubscribe; |
42 |
| - unsubscribe(f: Listener<K>): void; |
| 38 | +export interface Store<K, E> { |
| 39 | + action(action: Action<K, E>): Promise<void> | void; |
| 40 | + setState<U extends keyof K>(update: Pick<K, U>, overwrite?: boolean, action?: Action<K, E>): void; |
| 41 | + subscribe(f: Listener<K, E>): Unsubscribe; |
| 42 | + unsubscribe(f: Listener<K, E>): void; |
43 | 43 | getState(): K;
|
44 | 44 | }
|
45 | 45 |
|
46 |
| -export default function createStore<K>(state?: K): Store<K>; |
| 46 | +export default function createStore<K, E>(state?: K, extraArg?: E): Store<K, E>; |
47 | 47 |
|
48 | 48 | export type StateMapper<T, K, I> = (state: K, props: T) => I;
|
0 commit comments