2
2
* External dependencies
3
3
*/
4
4
// eslint-disable-next-line no-restricted-imports
5
- import type { combineReducers as reduxCombineReducers } from 'redux' ;
5
+ import type { combineReducers as reduxCombineReducers , AnyAction } from 'redux' ;
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import type {
11
+ invalidateResolution ,
12
+ invalidateResolutionForStore ,
13
+ invalidateResolutionForStoreSelector ,
14
+ } from './redux-store/metadata/actions' ;
6
15
7
16
type MapOf < T > = { [ name : string ] : T } ;
8
17
@@ -43,6 +52,34 @@ export interface ReduxStoreConfig<
43
52
controls ?: MapOf < Function > ;
44
53
}
45
54
55
+ type InvalidateResolution = typeof invalidateResolution ;
56
+ type InvalidateResolutionForStore = typeof invalidateResolutionForStore ;
57
+ type InvalidateResolutionForStoreSelector =
58
+ typeof invalidateResolutionForStoreSelector ;
59
+
60
+ type InvalidateResolutionAction = ReturnType < InvalidateResolution > ;
61
+ type InvalidateResolutionForStoreAction =
62
+ ReturnType < InvalidateResolutionForStore > ;
63
+ type InvalidateResolutionForStoreSelectorAction =
64
+ ReturnType < InvalidateResolutionForStoreSelector > ;
65
+
66
+ /**
67
+ * The action creators for metadata actions.
68
+ */
69
+ type MetadataActionCreators = {
70
+ invalidateResolution : InvalidateResolution ;
71
+ invalidateResolutionForStore : InvalidateResolutionForStore ;
72
+ invalidateResolutionForStoreSelector : InvalidateResolutionForStoreSelector ;
73
+ } ;
74
+
75
+ /**
76
+ * Metadata actions.
77
+ */
78
+ type MetadataAction =
79
+ | InvalidateResolutionAction
80
+ | InvalidateResolutionForStoreAction
81
+ | InvalidateResolutionForStoreSelectorAction ;
82
+
46
83
// Return type for the useSelect() hook.
47
84
export type UseSelectReturn < F extends MapSelect | StoreDescriptor < any > > =
48
85
F extends MapSelect
@@ -158,7 +195,17 @@ export interface SelectorWithCustomCurrySignature {
158
195
}
159
196
160
197
export interface DataRegistry {
161
- register : ( store : StoreDescriptor < any > ) => void ;
198
+ register : ( store : StoreDescriptor < AnyConfig > ) => void ;
199
+ dispatch : < S extends string | StoreDescriptor < AnyConfig > > (
200
+ storeNameOrDescriptor : S
201
+ ) => S extends StoreDescriptor < infer Config >
202
+ ? ActionCreatorsOf < Config >
203
+ : unknown ;
204
+ select : < S extends string | StoreDescriptor < AnyConfig > > (
205
+ storeNameOrDescriptor : S
206
+ ) => S extends StoreDescriptor < infer Config >
207
+ ? CurriedSelectorsOf < Config >
208
+ : unknown ;
162
209
}
163
210
164
211
export interface DataEmitter {
@@ -173,11 +220,19 @@ export interface DataEmitter {
173
220
174
221
export type ConfigOf < S > = S extends StoreDescriptor < infer C > ? C : never ;
175
222
176
- export type ActionCreatorsOf < Config extends AnyConfig > =
223
+ export type BaseActionCreatorsOf < Config extends AnyConfig > =
177
224
Config extends ReduxStoreConfig < any , infer ActionCreators , any >
178
225
? PromisifiedActionCreators < ActionCreators >
179
226
: never ;
180
227
228
+ /**
229
+ * The action creators for a store config.
230
+ *
231
+ * Also includes metadata actions creators.
232
+ */
233
+ type ActionCreatorsOf < Config extends AnyConfig > =
234
+ BaseActionCreatorsOf < Config > & MetadataActionCreators ;
235
+
181
236
// Takes an object containing all action creators for a store and updates the
182
237
// return type of each action creator to account for internal registry details --
183
238
// for example, dispatched actions are wrapped with a Promise.
@@ -214,4 +269,40 @@ type SelectorsOf< Config extends AnyConfig > = Config extends ReduxStoreConfig<
214
269
? { [ name in keyof Selectors ] : Function }
215
270
: never ;
216
271
272
+ /**
273
+ * Thunk arguments.
274
+ *
275
+ * Used to type the arguments passed to a thunk function.
276
+ */
277
+ export type ThunkArgs <
278
+ Action extends AnyAction ,
279
+ S extends StoreDescriptor < AnyConfig > ,
280
+ > = {
281
+ /**
282
+ * Dispatch an action to the store.
283
+ */
284
+ dispatch : ( S extends StoreDescriptor < infer Config >
285
+ ? ActionCreatorsOf < Config >
286
+ : unknown ) &
287
+ ( ( action : Action | MetadataAction ) => void ) ;
288
+
289
+ /**
290
+ * Selectors for the store.
291
+ */
292
+ select : CurriedSelectorsOf < S > ;
293
+
294
+ /**
295
+ * The store registry object.
296
+ */
297
+ registry : DataRegistry ;
298
+ } ;
299
+
300
+ export type Thunk <
301
+ A extends AnyAction ,
302
+ S extends StoreDescriptor < AnyConfig > ,
303
+ T extends unknown = void ,
304
+ > = T extends Awaited < infer R >
305
+ ? ( args : ThunkArgs < A , S > ) => Promise < R >
306
+ : ( args : ThunkArgs < A , S > ) => T ;
307
+
217
308
export type combineReducers = typeof reduxCombineReducers ;
0 commit comments