tutu/types/lib.esnext.promise.d.ts
2024-12-04 22:57:41 +08:00

26 lines
774 B
TypeScript

interface AnyPromiseWithResolvers<T, Instance> {
promise: Instance;
resolve: (value: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
}
type AnyPromiseConstructor<T> = new (
executor: (
resolve: PromiseWithResolvers<T>["resolve"],
reject: PromiseWithResolvers<T>["reject"],
) => void,
) => Promise<T>;
interface PromiseConstructor {
/**
* Creates a new Promise and returns it in an object, along with its resolve and reject functions.
* @returns An object with the properties `promise`, `resolve`, and `reject`.
*
* ```ts
* const { promise, resolve, reject } = Promise.withResolvers<T>();
* ```
*/
withResolvers<T, K extends AnyPromiseConstructor<T>>(
this: K,
): AnyPromiseWithResolvers<T, InstanceType<K>>;
}