1
+
1
2
/**
2
3
* An *action* is a plain object that represents an intention to change the
3
4
* state. Actions are the only way to get data into the store. Any data,
12
13
* Other than `type`, the structure of an action object is really up to you.
13
14
* If you're interested, check out Flux Standard Action for recommendations on
14
15
* how actions should be constructed.
16
+ *
17
+ * @template T the type of the action's `type` tag.
15
18
*/
16
- export interface Action {
17
- type : any ;
19
+ export interface Action < T = any > {
20
+ type : T ;
18
21
}
19
22
20
-
21
23
/* reducers */
22
24
23
25
/**
@@ -41,15 +43,18 @@ export interface Action {
41
43
*
42
44
* *Do not put API calls into reducers.*
43
45
*
44
- * @template S State object type.
46
+ * @template S The type of state consumed and produced by this reducer.
47
+ * @template A The type of actions the reducer can potentially respond to.
45
48
*/
46
- export type Reducer < S > = < A extends Action > ( state : S | undefined , action : A ) => S ;
49
+ export type Reducer < S = { } , A extends Action = Action > = ( state : S | undefined , action : A ) => S ;
47
50
48
51
/**
49
52
* Object whose values correspond to different reducer functions.
53
+ *
54
+ * @template A The type of actions the reducers can potentially respond to.
50
55
*/
51
- export type ReducersMapObject < S > = {
52
- [ K in keyof S ] : Reducer < S [ K ] > ;
56
+ export type ReducersMapObject < S = { } , A extends Action = Action > = {
57
+ [ K in keyof S ] : Reducer < S [ K ] , A > ;
53
58
}
54
59
55
60
/**
@@ -70,7 +75,7 @@ export type ReducersMapObject<S> = {
70
75
* @returns A reducer function that invokes every reducer inside the passed
71
76
* object, and builds a state object with the same shape.
72
77
*/
73
- export function combineReducers < S > ( reducers : ReducersMapObject < S > ) : Reducer < S > ;
78
+ export function combineReducers < S , A extends Action > ( reducers : ReducersMapObject < S , A > ) : Reducer < S , A > ;
74
79
75
80
76
81
/* store */
@@ -92,9 +97,12 @@ export function combineReducers<S>(reducers: ReducersMapObject<S>): Reducer<S>;
92
97
* function to handle async actions in addition to actions. Middleware may
93
98
* transform, delay, ignore, or otherwise interpret actions or async actions
94
99
* before passing them to the next middleware.
100
+ *
101
+ * @template S unused, here only for backwards compatibility.
102
+ * @template D the type of things (actions or otherwise) which may be dispatched.
95
103
*/
96
- export interface Dispatch < S > {
97
- < A extends Action > ( action : A ) : A ;
104
+ export interface Dispatch < S = any , D = Action > {
105
+ < A extends D > ( action : A ) : A ;
98
106
}
99
107
100
108
/**
@@ -109,9 +117,11 @@ export interface Unsubscribe {
109
117
* There should only be a single store in a Redux app, as the composition
110
118
* happens on the reducer level.
111
119
*
112
- * @template S State object type.
120
+ * @template S The type of state held by this store.
121
+ * @template A the type of actions which may be dispatched by this store.
122
+ * @template N The type of non-actions which may be dispatched by this store.
113
123
*/
114
- export interface Store < S > {
124
+ export interface Store < S = { } , A extends Action = Action , N = never > {
115
125
/**
116
126
* Dispatches an action. It is the only way to trigger a state change.
117
127
*
@@ -138,7 +148,7 @@ export interface Store<S> {
138
148
* Note that, if you use a custom middleware, it may wrap `dispatch()` to
139
149
* return something else (for example, a Promise you can await).
140
150
*/
141
- dispatch : Dispatch < S > ;
151
+ dispatch : Dispatch < any , A | N > ;
142
152
143
153
/**
144
154
* Reads the state tree managed by the store.
@@ -182,7 +192,7 @@ export interface Store<S> {
182
192
*
183
193
* @param nextReducer The reducer for the store to use instead.
184
194
*/
185
- replaceReducer ( nextReducer : Reducer < S > ) : void ;
195
+ replaceReducer ( nextReducer : Reducer < S , A > ) : void ;
186
196
}
187
197
188
198
/**
@@ -191,11 +201,13 @@ export interface Store<S> {
191
201
* `createStore(reducer, preloadedState)` exported from the Redux package, from
192
202
* store creators that are returned from the store enhancers.
193
203
*
194
- * @template S State object type.
204
+ * @template S The type of state to be held by the store.
205
+ * @template A The type of actions which may be dispatched.
206
+ * @template D The type of all things which may be dispatched.
195
207
*/
196
208
export interface StoreCreator {
197
- < S > ( reducer : Reducer < S > , enhancer ?: StoreEnhancer < S > ) : Store < S > ;
198
- < S > ( reducer : Reducer < S > , preloadedState : S , enhancer ?: StoreEnhancer < S > ) : Store < S > ;
209
+ < S , A extends Action , N > ( reducer : Reducer < S , A > , enhancer ?: StoreEnhancer < N > ) : Store < S , A , N > ;
210
+ < S , A extends Action , N > ( reducer : Reducer < S , A > , preloadedState : S , enhancer ?: StoreEnhancer < N > ) : Store < S , A , N > ;
199
211
}
200
212
201
213
/**
@@ -215,10 +227,11 @@ export interface StoreCreator {
215
227
* provided by the developer tools. It is what makes time travel possible
216
228
* without the app being aware it is happening. Amusingly, the Redux
217
229
* middleware implementation is itself a store enhancer.
230
+ *
218
231
*/
219
- export type StoreEnhancer < S > = ( next : StoreEnhancerStoreCreator < S > ) => StoreEnhancerStoreCreator < S > ;
220
- export type GenericStoreEnhancer = < S > ( next : StoreEnhancerStoreCreator < S > ) => StoreEnhancerStoreCreator < S > ;
221
- export type StoreEnhancerStoreCreator < S > = ( reducer : Reducer < S > , preloadedState ?: S ) => Store < S > ;
232
+ export type StoreEnhancer < N = never > = ( next : StoreEnhancerStoreCreator < N > ) => StoreEnhancerStoreCreator < N > ;
233
+ export type GenericStoreEnhancer < N = never > = StoreEnhancer < N > ;
234
+ export type StoreEnhancerStoreCreator < N = never > = < S = any , A extends Action = Action > ( reducer : Reducer < S , A > , preloadedState ?: S ) => Store < S , A , N > ;
222
235
223
236
/**
224
237
* Creates a Redux store that holds the state tree.
@@ -253,8 +266,8 @@ export const createStore: StoreCreator;
253
266
254
267
/* middleware */
255
268
256
- export interface MiddlewareAPI < S > {
257
- dispatch : Dispatch < S > ;
269
+ export interface MiddlewareAPI < S = any , D = Action > {
270
+ dispatch : Dispatch < any , D > ;
258
271
getState ( ) : S ;
259
272
}
260
273
@@ -268,7 +281,7 @@ export interface MiddlewareAPI<S> {
268
281
* asynchronous API call into a series of synchronous actions.
269
282
*/
270
283
export interface Middleware {
271
- < S > ( api : MiddlewareAPI < S > ) : ( next : Dispatch < S > ) => Dispatch < S > ;
284
+ < S = any , D = Action > ( api : MiddlewareAPI < S , D > ) : ( next : Dispatch < any , D > ) => Dispatch < any , D > ;
272
285
}
273
286
274
287
/**
@@ -317,8 +330,8 @@ export interface ActionCreator<A> {
317
330
/**
318
331
* Object whose values are action creator functions.
319
332
*/
320
- export interface ActionCreatorsMapObject {
321
- [ key : string ] : ActionCreator < any > ;
333
+ export interface ActionCreatorsMapObject < A = any > {
334
+ [ key : string ] : ActionCreator < A > ;
322
335
}
323
336
324
337
/**
@@ -340,19 +353,19 @@ export interface ActionCreatorsMapObject {
340
353
* creator wrapped into the `dispatch` call. If you passed a function as
341
354
* `actionCreator`, the return value will also be a single function.
342
355
*/
343
- export function bindActionCreators < A extends ActionCreator < any > > ( actionCreator : A , dispatch : Dispatch < any > ) : A ;
356
+ export function bindActionCreators < A , C extends ActionCreator < A > > ( actionCreator : C , dispatch : Dispatch < any , A > ) : C ;
344
357
345
358
export function bindActionCreators <
346
359
A extends ActionCreator < any > ,
347
360
B extends ActionCreator < any >
348
- > ( actionCreator : A , dispatch : Dispatch < any > ) : B ;
361
+ > ( actionCreator : A , dispatch : Dispatch < any , any > ) : B ;
349
362
350
- export function bindActionCreators < M extends ActionCreatorsMapObject > ( actionCreators : M , dispatch : Dispatch < any > ) : M ;
363
+ export function bindActionCreators < A , M extends ActionCreatorsMapObject < A > > ( actionCreators : M , dispatch : Dispatch < any , A > ) : M ;
351
364
352
365
export function bindActionCreators <
353
- M extends ActionCreatorsMapObject ,
354
- N extends ActionCreatorsMapObject
355
- > ( actionCreators : M , dispatch : Dispatch < any > ) : N ;
366
+ M extends ActionCreatorsMapObject < any > ,
367
+ N extends ActionCreatorsMapObject < any >
368
+ > ( actionCreators : M , dispatch : Dispatch < any , any > ) : N ;
356
369
357
370
358
371
/* compose */
0 commit comments