Skip to content

Commit 68e510c

Browse files
committed
fix: improve data package types
This commit improves the types in the data package. - Adds new types `ThunkArgs` and `Thunk`. These types are intended to be used by consumers of the data package to define their own thunks. - Adds meta action creators to the `ActionCreatorsOf` type. - Adds the `dispatch` and `select` functions to the `DataRegistry` type. - Adds the `invalidate` action creators to the `ActionCreatorsOf` type. This allows consumers of the data package to have more complete type safety. Also, provides access to the invalidate resolution action creators, which are useful for complex data store manipulation. Adding new types as described above. Building the package types.
1 parent 636710b commit 68e510c

File tree

1 file changed

+94
-3
lines changed

1 file changed

+94
-3
lines changed

packages/data/src/types.ts

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
* External dependencies
33
*/
44
// 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';
615

716
type MapOf< T > = { [ name: string ]: T };
817

@@ -43,6 +52,34 @@ export interface ReduxStoreConfig<
4352
controls?: MapOf< Function >;
4453
}
4554

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+
4683
// Return type for the useSelect() hook.
4784
export type UseSelectReturn< F extends MapSelect | StoreDescriptor< any > > =
4885
F extends MapSelect
@@ -158,7 +195,17 @@ export interface SelectorWithCustomCurrySignature {
158195
}
159196

160197
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;
162209
}
163210

164211
export interface DataEmitter {
@@ -173,11 +220,19 @@ export interface DataEmitter {
173220

174221
export type ConfigOf< S > = S extends StoreDescriptor< infer C > ? C : never;
175222

176-
export type ActionCreatorsOf< Config extends AnyConfig > =
223+
export type BaseActionCreatorsOf< Config extends AnyConfig > =
177224
Config extends ReduxStoreConfig< any, infer ActionCreators, any >
178225
? PromisifiedActionCreators< ActionCreators >
179226
: never;
180227

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+
181236
// Takes an object containing all action creators for a store and updates the
182237
// return type of each action creator to account for internal registry details --
183238
// for example, dispatched actions are wrapped with a Promise.
@@ -214,4 +269,40 @@ type SelectorsOf< Config extends AnyConfig > = Config extends ReduxStoreConfig<
214269
? { [ name in keyof Selectors ]: Function }
215270
: never;
216271

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+
217308
export type combineReducers = typeof reduxCombineReducers;

0 commit comments

Comments
 (0)