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

Interactivity API: Convert types of generator functions to async functions #62400

Closed
22 changes: 20 additions & 2 deletions packages/interactivity/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ interface StoreOptions {
lock?: boolean | string;
}

// Utility type to check if a function is a generator function.
type IsGenerator< T > = T extends (
...args: infer A
) => Generator< infer R, any, any >
? ( ...args: A ) => Promise< R >
: never;
luisherranz marked this conversation as resolved.
Show resolved Hide resolved

// Utility type to convert all generator functions in an object to async functions.
type ConvertGenerators< T > = {
[ K in keyof T ]: T[ K ] extends ( ...args: any[] ) => any
? IsGenerator< T[ K ] > extends never
? T[ K ]
: IsGenerator< T[ K ] >
: T[ K ] extends object
? ConvertGenerators< T[ K ] >
: T[ K ];
};

export const universalUnlock =
'I acknowledge that using a private store means my plugin will inevitably break on the next store release.';

Expand Down Expand Up @@ -259,13 +277,13 @@ export function store< S extends object = {} >(
namespace: string,
storePart?: S,
options?: StoreOptions
): S;
): ConvertGenerators< S >;

export function store< T extends object >(
namespace: string,
storePart?: T,
options?: StoreOptions
): T;
): ConvertGenerators< T >;

export function store(
namespace: string,
Expand Down
Loading