Skip to content
Next Next commit
Promisify action creators in ActionCreatorsOf type
  • Loading branch information
noahtallen committed Jul 20, 2023
commit 5162962a5c1f7f17c4f2f19d2bda051353839a60
12 changes: 10 additions & 2 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { combineReducers as reduxCombineReducers } from 'redux';

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

export type ActionCreator = Function | Generator;
export type ActionCreator = ( ...args: any[] ) => any | Generator;
Copy link
Member Author

@noahtallen noahtallen Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function doesn't let you add return types and isn't compatible with the arrow syntax, so it needs to be changed so that we can wrap around the type below.

export type Resolver = Function | Generator;
export type Selector = Function;

Expand Down Expand Up @@ -170,9 +170,17 @@ export type ConfigOf< S > = S extends StoreDescriptor< infer C > ? C : never;

export type ActionCreatorsOf< Config extends AnyConfig > =
Config extends ReduxStoreConfig< any, infer ActionCreators, any >
? ActionCreators
? PromisifiedActionCreator< 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 >;
};

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