Enforce Typecheck #51

Merged
Rubicon merged 5 commits from enforce-typecheck into master 2024-12-04 17:27:20 +00:00
2 changed files with 47 additions and 0 deletions
Showing only changes of commit 0fca81dc93 - Show all commits

View file

@ -15,3 +15,24 @@ if (typeof window.crypto.randomUUID === "undefined") {
) as `${string}-${string}-${string}-${string}-${string}`; ) as `${string}-${string}-${string}-${string}-${string}`;
}; };
} }
if (typeof Promise.withResolvers === "undefined") {
// Chrome/Edge 119, Firefox 121, Safari/iOS 17.4
// Promise.withResolvers is generic and works with subclasses - the typescript built-in decl
// could not handle the subclassing case.
Promise.withResolvers = function <T>(this: AnyPromiseConstructor<T>) {
let resolve!: PromiseWithResolvers<T>["resolve"], reject!: PromiseWithResolvers<T>["reject"];
// These variables are expected to be set after `new this()`
const promise = new this((resolve0, reject0) => {
resolve = resolve0;
reject = reject0;
})
return {
promise, resolve, reject
}
}
}

26
types/lib.esnext.promise.d.ts vendored Normal file
View file

@ -0,0 +1,26 @@
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>>;
}