Skip to content
Prev Previous commit
Next Next commit
Update PromisifiedActionCreator to handle thunks
  • Loading branch information
noahtallen committed Jul 20, 2023
commit 006048efbb98a497c5f62a985f3ab1477cfa1745
18 changes: 14 additions & 4 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ export interface ReduxStoreConfig<
controls?: MapOf< Function >;
}

// Return type for the useSelect() hook.
export type UseSelectReturn< F extends MapSelect | StoreDescriptor< any > > =
F extends MapSelect
? ReturnType< F >
: F extends StoreDescriptor< any >
? CurriedSelectorsOf< F >
: never;

// Return type for the useDispatch() hook.
export type UseDispatchReturn< StoreNameOrDescriptor > =
StoreNameOrDescriptor extends StoreDescriptor< any >
? ActionCreatorsOf< ConfigOf< StoreNameOrDescriptor > >
Expand All @@ -59,9 +61,12 @@ export type UseDispatchReturn< StoreNameOrDescriptor > =

export type DispatchFunction = < StoreNameOrDescriptor >(
store: StoreNameOrDescriptor
) => StoreNameOrDescriptor extends StoreDescriptor< any >
? ActionCreatorsOf< ConfigOf< StoreNameOrDescriptor > >
: any;
) => DispatchReturn< StoreNameOrDescriptor >;

export type DispatchReturn< StoreNameOrDescriptor > =
StoreNameOrDescriptor extends StoreDescriptor< any >
? ActionCreatorsOf< ConfigOf< StoreNameOrDescriptor > >
: unknown;

export type MapSelect = (
select: SelectFunction,
Expand Down Expand Up @@ -185,7 +190,12 @@ export type PromisifiedActionCreators<
// creator, so that consumers know that they are dealing with a Promise.
export type PromisifyActionCreator< Action extends ActionCreator > = (
...args: Parameters< Action >
) => Promise< ReturnType< Action > >;
) => Promise<
ReturnType< Action > extends ( ..._args: any[] ) => any
? // Thunks return another function, so we unwrap the return type twice in that scenario.
ReturnType< ReturnType< Action > >
: ReturnType< Action >
>;

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