Skip to content

Commit 0f6856a

Browse files
improve types
1 parent 3eb5e42 commit 0f6856a

File tree

1 file changed

+107
-27
lines changed

1 file changed

+107
-27
lines changed

Diff for: index.d.ts

+107-27
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,130 @@
1-
import { Unsubscribe } from 'redux';
1+
import { Action, AnyAction, Unsubscribe } from 'redux';
22

33
declare namespace ngRedux {
4-
export interface Reducer extends Function {
5-
(state: any, action: any): any;
6-
}
74

8-
export interface Dispatch extends Function {
9-
(action: any): any;
5+
export type Reducer<S = any, A extends Action = AnyAction> = (state: S | undefined, action: A) => S;
6+
7+
export interface Dispatch<A extends Action = AnyAction> {
8+
<T extends A>(action: T): T;
109
}
1110

12-
export interface MiddlewareArg {
13-
dispatch: Dispatch;
14-
getState: Function;
11+
export interface MiddlewareArg<D extends Dispatch = Dispatch, S = any> {
12+
dispatch: D;
13+
getState(): S;
1514
}
1615

17-
export interface Middleware extends Function {
18-
(obj: MiddlewareArg): Function;
16+
export interface Middleware<DispatchExt = {}, S = any, D extends Dispatch = Dispatch> {
17+
(api: MiddlewareArg<D, S>): (next: Dispatch<AnyAction>) => (action: any) => any;
1918
}
2019

20+
/* config */
21+
2122
export interface Config {
22-
debounce: DebounceConfig;
23+
debounce: DebounceConfig;
2324
}
24-
25+
2526
export interface DebounceConfig {
26-
wait?: number;
27-
maxWait?: number;
27+
wait?: number;
28+
maxWait?: number;
2829
}
2930

31+
/* API */
32+
3033
export interface INgRedux {
31-
getReducer(): Reducer;
32-
replaceReducer(nextReducer: Reducer): void;
33-
dispatch(action: any): any;
34-
getState(): any;
35-
subscribe(listener: Function): Unsubscribe;
36-
connect(
37-
mapStateToTarget: (state: any) => Object,
38-
mapDispatchToTarget?: Object | ((dispatch: Function) => Object)
39-
): (target: Function | Object) => () => void;
34+
/**
35+
* Replaces the reducer currently used by the store to calculate the state.
36+
*
37+
* You might need this if your app implements code splitting and you want to
38+
* load some of the reducers dynamically. You might also need this if you
39+
* implement a hot reloading mechanism for Redux.
40+
*
41+
* @param nextReducer The reducer for the store to use instead.
42+
*/
43+
replaceReducer(nextReducer: Reducer): void;
44+
/**
45+
* A *dispatching function* (or simply *dispatch function*) is a function that
46+
* accepts an action or an async action; it then may or may not dispatch one
47+
* or more actions to the store.
48+
*
49+
* We must distinguish between dispatching functions in general and the base
50+
* `dispatch` function provided by the store instance without any middleware.
51+
*
52+
* The base dispatch function *always* synchronously sends an action to the
53+
* store's reducer, along with the previous state returned by the store, to
54+
* calculate a new state. It expects actions to be plain objects ready to be
55+
* consumed by the reducer.
56+
*
57+
* Middleware wraps the base dispatch function. It allows the dispatch
58+
* function to handle async actions in addition to actions. Middleware may
59+
* transform, delay, ignore, or otherwise interpret actions or async actions
60+
* before passing them to the next middleware.
61+
*
62+
* @template A The type of things (actions or otherwise) which may be
63+
* dispatched.
64+
*/
65+
dispatch<A extends Action>(action: A): A;
66+
/**
67+
* Reads the state tree managed by the store.
68+
*
69+
* @returns The current state tree of your application.
70+
*/
71+
getState<S = any>(): S;
72+
/**
73+
* Adds a change listener. It will be called any time an action is
74+
* dispatched, and some part of the state tree may potentially have changed.
75+
* You may then call `getState()` to read the current state tree inside the
76+
* callback.
77+
*
78+
* You may call `dispatch()` from a change listener, with the following
79+
* caveats:
80+
*
81+
* 1. The subscriptions are snapshotted just before every `dispatch()` call.
82+
* If you subscribe or unsubscribe while the listeners are being invoked,
83+
* this will not have any effect on the `dispatch()` that is currently in
84+
* progress. However, the next `dispatch()` call, whether nested or not,
85+
* will use a more recent snapshot of the subscription list.
86+
*
87+
* 2. The listener should not expect to see all states changes, as the state
88+
* might have been updated multiple times during a nested `dispatch()` before
89+
* the listener is called. It is, however, guaranteed that all subscribers
90+
* registered before the `dispatch()` started will be called with the latest
91+
* state by the time it exits.
92+
*
93+
* @param listener A callback to be invoked on every dispatch.
94+
* @returns A function to remove this change listener.
95+
*/
96+
subscribe(listener: () => void): Unsubscribe;
97+
/**
98+
* Connects a component to a Redux store.
99+
*
100+
* @param mapStateToTarget
101+
* @param mapDispatchToTarget
102+
*/
103+
connect(
104+
mapStateToTarget: null | ((state: any) => { [key: string]: any; }),
105+
mapDispatchToTarget?: object | ((dispatch: Function) => object)
106+
): (target: Function | object) => Unsubscribe;
40107
}
41108

109+
/* provider */
110+
42111
export interface INgReduxProvider {
43-
createStoreWith(reducer: Reducer, middlewares?: Array<Middleware | string>, storeEnhancers?: Function[], initialState?: any): void;
44-
config: Config;
112+
/**
113+
* Creates Redux store.
114+
*
115+
* @param reducer
116+
* @param middlewares
117+
* @param storeEnhancers
118+
* @param initialState
119+
*/
120+
createStoreWith<S = any, I = any>(reducer: Reducer<S>, middlewares?: (Middleware | string)[], storeEnhancers?: Function[], initialState?: I): void;
121+
/**
122+
* ngRedux config object
123+
*/
124+
config: Config;
45125
}
46126
}
47127

48128
declare var ngRedux: string;
49129
export as namespace ngRedux;
50-
export default ngRedux;
130+
export default ngRedux;

0 commit comments

Comments
 (0)