Skip to content
Prev Previous commit
Next Next commit
Add singular and plural helper types
  • Loading branch information
noahtallen committed Jul 20, 2023
commit 943ff9c839fb2ee0f93e6c6cf8d4c6d1de47efd7
22 changes: 14 additions & 8 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,22 @@ export type ConfigOf< S > = S extends StoreDescriptor< infer C > ? C : never;

export type ActionCreatorsOf< Config extends AnyConfig > =
Config extends ReduxStoreConfig< any, infer ActionCreators, any >
? PromisifiedActionCreator< ActionCreators >
? PromisifiedActionCreators< ActionCreators >
: never;

// When dispatching an action creator, the return value is a promise.
type PromisifiedActionCreator< ActionCreators extends MapOf< ActionCreator > > =
{
[ Action in keyof ActionCreators ]: (
...args: Parameters< ActionCreators[ Action ] >
) => Promise< void >;
};
export type PromisifiedActionCreators<
ActionCreators extends MapOf< ActionCreator >
> = {
[ Action in keyof ActionCreators ]: PromisifyActionCreator<
ActionCreators[ Action ]
>;
};

// A dispatched action returns a Promise. This helper extends the original action
// creator, so that consumers know that they are dealing with a Promise.
export type PromisifyActionCreator< Action extends ActionCreator > = (
...args: Parameters< Action >
) => Promise< void >;

type SelectorsOf< Config extends AnyConfig > = Config extends ReduxStoreConfig<
any,
Expand Down