Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promisify action creator return type for WP data dispatch #52530

Merged
merged 11 commits into from
Jul 20, 2023
Next Next commit
Promisify action creators in ActionCreatorsOf type
noahtallen committed Jul 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 5162962a5c1f7f17c4f2f19d2bda051353839a60
12 changes: 10 additions & 2 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
@@ -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;

@@ -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 > > =
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
{
[ Action in keyof ActionCreators ]: (
...args: Parameters< ActionCreators[ Action ] >
) => Promise< void >;
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
};

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