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 ;
18
- }
19
-
20
- /**
21
- * An Action type which accepts any other properties.
22
- * This is mainly for the use of the `Reducer` type.
23
- * This is not part of `Action` itself to prevent users who are extending `Action.
24
- */
25
- export interface AnyAction extends Action {
26
- // Allows any extra properties to be defined in an action.
27
- [ extraProps : string ] : any ;
19
+ export interface Action < T = any > {
20
+ type : T ;
28
21
}
29
22
30
-
31
23
/* reducers */
32
24
33
25
/**
@@ -51,15 +43,18 @@ export interface AnyAction extends Action {
51
43
*
52
44
* *Do not put API calls into reducers.*
53
45
*
54
- * @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.
55
48
*/
56
- export type Reducer < S > = ( state : S , action : AnyAction ) => S ;
49
+ export type Reducer < S = any , A extends Action = Action > = ( state : S | undefined , action : A ) => S ;
57
50
58
51
/**
59
52
* Object whose values correspond to different reducer functions.
53
+ *
54
+ * @template A The type of actions the reducers can potentially respond to.
60
55
*/
61
- export type ReducersMapObject < S > = {
62
- [ K in keyof S ] : Reducer < S [ K ] > ;
56
+ export type ReducersMapObject < S = any , A extends Action = Action > = {
57
+ [ K in keyof S ] : Reducer < S [ K ] , A > ;
63
58
}
64
59
65
60
/**
@@ -80,7 +75,7 @@ export type ReducersMapObject<S> = {
80
75
* @returns A reducer function that invokes every reducer inside the passed
81
76
* object, and builds a state object with the same shape.
82
77
*/
83
- export function combineReducers < S > ( reducers : ReducersMapObject < S > ) : Reducer < S > ;
78
+ export function combineReducers < S , A extends Action = Action > ( reducers : ReducersMapObject < S , A > ) : Reducer < S , A > ;
84
79
85
80
86
81
/* store */
@@ -102,9 +97,11 @@ export function combineReducers<S>(reducers: ReducersMapObject<S>): Reducer<S>;
102
97
* function to handle async actions in addition to actions. Middleware may
103
98
* transform, delay, ignore, or otherwise interpret actions or async actions
104
99
* before passing them to the next middleware.
100
+ *
101
+ * @template D the type of things (actions or otherwise) which may be dispatched.
105
102
*/
106
- export interface Dispatch < S > {
107
- < A extends Action > ( action : A ) : A ;
103
+ export interface Dispatch < D = Action > {
104
+ < A extends D > ( action : A ) : A ;
108
105
}
109
106
110
107
/**
@@ -119,9 +116,11 @@ export interface Unsubscribe {
119
116
* There should only be a single store in a Redux app, as the composition
120
117
* happens on the reducer level.
121
118
*
122
- * @template S State object type.
119
+ * @template S The type of state held by this store.
120
+ * @template A the type of actions which may be dispatched by this store.
121
+ * @template N The type of non-actions which may be dispatched by this store.
123
122
*/
124
- export interface Store < S > {
123
+ export interface Store < S = any , A extends Action = Action , N = never > {
125
124
/**
126
125
* Dispatches an action. It is the only way to trigger a state change.
127
126
*
@@ -148,7 +147,7 @@ export interface Store<S> {
148
147
* Note that, if you use a custom middleware, it may wrap `dispatch()` to
149
148
* return something else (for example, a Promise you can await).
150
149
*/
151
- dispatch : Dispatch < S > ;
150
+ dispatch : Dispatch < A | N > ;
152
151
153
152
/**
154
153
* Reads the state tree managed by the store.
@@ -192,7 +191,7 @@ export interface Store<S> {
192
191
*
193
192
* @param nextReducer The reducer for the store to use instead.
194
193
*/
195
- replaceReducer ( nextReducer : Reducer < S > ) : void ;
194
+ replaceReducer ( nextReducer : Reducer < S , A > ) : void ;
196
195
}
197
196
198
197
/**
@@ -201,11 +200,13 @@ export interface Store<S> {
201
200
* `createStore(reducer, preloadedState)` exported from the Redux package, from
202
201
* store creators that are returned from the store enhancers.
203
202
*
204
- * @template S State object type.
203
+ * @template S The type of state to be held by the store.
204
+ * @template A The type of actions which may be dispatched.
205
+ * @template D The type of all things which may be dispatched.
205
206
*/
206
207
export interface StoreCreator {
207
- < S > ( reducer : Reducer < S > , enhancer ?: StoreEnhancer < S > ) : Store < S > ;
208
- < S > ( reducer : Reducer < S > , preloadedState : S , enhancer ?: StoreEnhancer < S > ) : Store < S > ;
208
+ < S , A extends Action , N > ( reducer : Reducer < S , A > , enhancer ?: StoreEnhancer < N > ) : Store < S , A , N > ;
209
+ < S , A extends Action , N > ( reducer : Reducer < S , A > , preloadedState : S , enhancer ?: StoreEnhancer < N > ) : Store < S , A , N > ;
209
210
}
210
211
211
212
/**
@@ -225,10 +226,11 @@ export interface StoreCreator {
225
226
* provided by the developer tools. It is what makes time travel possible
226
227
* without the app being aware it is happening. Amusingly, the Redux
227
228
* middleware implementation is itself a store enhancer.
229
+ *
228
230
*/
229
- export type StoreEnhancer < S > = ( next : StoreEnhancerStoreCreator < S > ) => StoreEnhancerStoreCreator < S > ;
230
- export type GenericStoreEnhancer = < S > ( next : StoreEnhancerStoreCreator < S > ) => StoreEnhancerStoreCreator < S > ;
231
- export type StoreEnhancerStoreCreator < S > = ( reducer : Reducer < S > , preloadedState ?: S ) => Store < S > ;
231
+ export type StoreEnhancer < N = never > = ( next : StoreEnhancerStoreCreator < N > ) => StoreEnhancerStoreCreator < N > ;
232
+ export type GenericStoreEnhancer < N = never > = StoreEnhancer < N > ;
233
+ export type StoreEnhancerStoreCreator < N = never > = < S = any , A extends Action = Action > ( reducer : Reducer < S , A > , preloadedState ?: S ) => Store < S , A , N > ;
232
234
233
235
/**
234
236
* Creates a Redux store that holds the state tree.
@@ -263,8 +265,8 @@ export const createStore: StoreCreator;
263
265
264
266
/* middleware */
265
267
266
- export interface MiddlewareAPI < S > {
267
- dispatch : Dispatch < S > ;
268
+ export interface MiddlewareAPI < S = any , D = Action > {
269
+ dispatch : Dispatch < D > ;
268
270
getState ( ) : S ;
269
271
}
270
272
@@ -278,7 +280,7 @@ export interface MiddlewareAPI<S> {
278
280
* asynchronous API call into a series of synchronous actions.
279
281
*/
280
282
export interface Middleware {
281
- < S > ( api : MiddlewareAPI < S > ) : ( next : Dispatch < S > ) => Dispatch < S > ;
283
+ < S = any , D = Action > ( api : MiddlewareAPI < S , D > ) : ( next : Dispatch < D > ) => Dispatch < D > ;
282
284
}
283
285
284
286
/**
@@ -327,8 +329,8 @@ export interface ActionCreator<A> {
327
329
/**
328
330
* Object whose values are action creator functions.
329
331
*/
330
- export interface ActionCreatorsMapObject {
331
- [ key : string ] : ActionCreator < any > ;
332
+ export interface ActionCreatorsMapObject < A = any > {
333
+ [ key : string ] : ActionCreator < A > ;
332
334
}
333
335
334
336
/**
@@ -350,18 +352,18 @@ export interface ActionCreatorsMapObject {
350
352
* creator wrapped into the `dispatch` call. If you passed a function as
351
353
* `actionCreator`, the return value will also be a single function.
352
354
*/
353
- export function bindActionCreators < A extends ActionCreator < any > > ( actionCreator : A , dispatch : Dispatch < any > ) : A ;
355
+ export function bindActionCreators < A , C extends ActionCreator < A > > ( actionCreator : C , dispatch : Dispatch < A > ) : C ;
354
356
355
357
export function bindActionCreators <
356
358
A extends ActionCreator < any > ,
357
359
B extends ActionCreator < any >
358
360
> ( actionCreator : A , dispatch : Dispatch < any > ) : B ;
359
361
360
- export function bindActionCreators < M extends ActionCreatorsMapObject > ( actionCreators : M , dispatch : Dispatch < any > ) : M ;
362
+ export function bindActionCreators < A , M extends ActionCreatorsMapObject < A > > ( actionCreators : M , dispatch : Dispatch < A > ) : M ;
361
363
362
364
export function bindActionCreators <
363
- M extends ActionCreatorsMapObject ,
364
- N extends ActionCreatorsMapObject
365
+ M extends ActionCreatorsMapObject < any > ,
366
+ N extends ActionCreatorsMapObject < any >
365
367
> ( actionCreators : M , dispatch : Dispatch < any > ) : N ;
366
368
367
369
0 commit comments