diff --git a/docs/diff.md b/docs/diff.md index bd05463..07884bf 100644 --- a/docs/diff.md +++ b/docs/diff.md @@ -16,8 +16,10 @@ The following files are improved in better-typescript-lib: - [es2017.object.d.ts](./diff/es2017.object.d.ts.md) - [es2018.asyncgenerator.d.ts](./diff/es2018.asyncgenerator.d.ts.md) - [es2018.asynciterable.d.ts](./diff/es2018.asynciterable.d.ts.md) +- [es2018.promise.d.ts](./diff/es2018.promise.d.ts.md) - [es2019.object.d.ts](./diff/es2019.object.d.ts.md) - [es2020.bigint.d.ts](./diff/es2020.bigint.d.ts.md) +- [es2020.promise.d.ts](./diff/es2020.promise.d.ts.md) - [es2021.string.d.ts](./diff/es2021.string.d.ts.md) - [es2021.promise.d.ts](./diff/es2021.promise.d.ts.md) - [es2022.object.d.ts](./diff/es2022.object.d.ts.md) diff --git a/docs/diff/es2015.promise.d.ts.md b/docs/diff/es2015.promise.d.ts.md index 2bbc407..4aa1b9f 100644 --- a/docs/diff/es2015.promise.d.ts.md +++ b/docs/diff/es2015.promise.d.ts.md @@ -5,7 +5,7 @@ Index: es2015.promise.d.ts =================================================================== --- es2015.promise.d.ts +++ es2015.promise.d.ts -@@ -1,19 +1,32 @@ +@@ -1,19 +1,20 @@ interface PromiseConstructor { /** * A reference to the prototype. @@ -23,20 +23,8 @@ Index: es2015.promise.d.ts executor: ( - resolve: (value: T | PromiseLike) => void, + resolve: undefined extends T -+ ? { -+ (value?: T | PromiseLike): void; -+ } -+ : { -+ (value: T | PromiseLike): void; -+ }, -+ // TODO: Revisit after https://github.com/microsoft/TypeScript/issues/42156 solves -+ // { -+ // (value: T | PromiseLike): void; -+ // } & (undefined extends T -+ // ? { -+ // (value?: T | PromiseLike): void; -+ // } -+ // : {}), ++ ? (value?: T | PromiseLike) => void ++ : (value: T | PromiseLike) => void, reject: (reason?: any) => void ) => void ): Promise; diff --git a/docs/diff/es2018.promise.d.ts.md b/docs/diff/es2018.promise.d.ts.md new file mode 100644 index 0000000..fe10e8f --- /dev/null +++ b/docs/diff/es2018.promise.d.ts.md @@ -0,0 +1,19 @@ +# es2018.promise.d.ts Diffs + +```diff +Index: es2018.promise.d.ts +=================================================================== +--- es2018.promise.d.ts ++++ es2018.promise.d.ts +@@ -7,6 +7,8 @@ + * resolved value cannot be modified from the callback. + * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). + * @returns A Promise for the completion of the callback. + */ +- finally(onfinally?: (() => void) | undefined | null): Promise; ++ finally( ++ onfinally?: (() => U | PromiseLike) | null | undefined ++ ): Promise; + } + +``` diff --git a/docs/diff/es2020.promise.d.ts.md b/docs/diff/es2020.promise.d.ts.md new file mode 100644 index 0000000..2304c31 --- /dev/null +++ b/docs/diff/es2020.promise.d.ts.md @@ -0,0 +1,41 @@ +# es2020.promise.d.ts Diffs + +```diff +Index: es2020.promise.d.ts +=================================================================== +--- es2020.promise.d.ts ++++ es2020.promise.d.ts +@@ -4,9 +4,9 @@ + } + + interface PromiseRejectedResult { + status: "rejected"; +- reason: any; ++ reason: unknown; + } + + type PromiseSettledResult = + | PromiseFulfilledResult +@@ -20,16 +20,18 @@ + * @returns A new Promise. + */ + allSettled( + values: T +- ): Promise<{ -readonly [P in keyof T]: PromiseSettledResult> }>; ++ ): Promise<{ ++ -readonly [P in keyof T]: PromiseSettledResult>; ++ }>; + + /** + * Creates a Promise that is resolved with an array of results when all + * of the provided Promises resolve or reject. + * @param values An array of Promises. + * @returns A new Promise. + */ + allSettled( +- values: Iterable> ++ values: Iterable + ): Promise>[]>; + } + +``` diff --git a/docs/diff/es2021.promise.d.ts.md b/docs/diff/es2021.promise.d.ts.md index 082b6f9..b00f11d 100644 --- a/docs/diff/es2021.promise.d.ts.md +++ b/docs/diff/es2021.promise.d.ts.md @@ -13,5 +13,13 @@ Index: es2021.promise.d.ts interface AggregateErrorConstructor { new (errors: Iterable, message?: string): AggregateError; +@@ -27,6 +27,6 @@ + * The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm. + * @param values An array or iterable of Promises. + * @returns A new Promise. + */ +- any(values: Iterable>): Promise>; ++ any(values: Iterable): Promise>; + } ``` diff --git a/docs/diff/es5.d.ts.md b/docs/diff/es5.d.ts.md index 980b84a..33f6efd 100644 --- a/docs/diff/es5.d.ts.md +++ b/docs/diff/es5.d.ts.md @@ -829,23 +829,106 @@ Index: es5.d.ts declare var Array: ArrayConstructor; -@@ -1737,9 +1776,15 @@ +@@ -1737,9 +1776,11 @@ } declare type PromiseConstructorLike = new ( executor: ( - resolve: (value: T | PromiseLike) => void, + resolve: undefined extends T -+ ? { -+ (value?: T | PromiseLike): void; -+ } -+ : { -+ (value: T | PromiseLike): void; -+ }, ++ ? (value?: T | PromiseLike) => void ++ : (value: T | PromiseLike) => void, reject: (reason?: any) => void ) => void ) => PromiseLike; +@@ -1749,52 +1790,56 @@ + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ +- then( +- onfulfilled?: +- | ((value: T) => TResult1 | PromiseLike) +- | undefined +- | null, +- onrejected?: +- | ((reason: any) => TResult2 | PromiseLike) +- | undefined +- | null +- ): PromiseLike; ++ then( ++ onfulfilled?: null | undefined, ++ onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined ++ ): PromiseLike; ++ ++ /** ++ * Attaches callbacks for the resolution and/or rejection of the Promise. ++ * @param onfulfilled The callback to execute when the Promise is resolved. ++ * @param onrejected The callback to execute when the Promise is rejected. ++ * @returns A Promise for the completion of which ever callback is executed. ++ */ ++ then( ++ onfulfilled: (value: T) => U | PromiseLike, ++ onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined ++ ): PromiseLike; + } + +-/** +- * Represents the completion of an asynchronous operation +- */ +-interface Promise { ++interface Promise extends PromiseLike { + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ +- then( +- onfulfilled?: +- | ((value: T) => TResult1 | PromiseLike) +- | undefined +- | null, +- onrejected?: +- | ((reason: any) => TResult2 | PromiseLike) +- | undefined +- | null +- ): Promise; ++ then( ++ onfulfilled?: null | undefined, ++ onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined ++ ): Promise; + + /** ++ * Attaches callbacks for the resolution and/or rejection of the Promise. ++ * @param onfulfilled The callback to execute when the Promise is resolved. ++ * @param onrejected The callback to execute when the Promise is rejected. ++ * @returns A Promise for the completion of which ever callback is executed. ++ */ ++ then( ++ onfulfilled: (value: T) => U | PromiseLike, ++ onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined ++ ): Promise; ++ ++ /** + * Attaches a callback for only the rejection of the Promise. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of the callback. + */ +- catch( +- onrejected?: +- | ((reason: any) => TResult | PromiseLike) +- | undefined +- | null +- ): Promise; ++ catch( ++ onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined ++ ): Promise; + } + + /** + * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`. @@ -2144,20 +2189,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. diff --git a/generated/lib.dom.d.ts b/generated/lib.dom.d.ts index 41fcd9e..8fb04c5 100644 --- a/generated/lib.dom.d.ts +++ b/generated/lib.dom.d.ts @@ -2826,13 +2826,7 @@ interface Body { json(): Promise; text(): Promise; } -// readonly body: ReadableStream | null; -// readonly bodyUsed: boolean; -// arrayBuffer(): Promise; -// blob(): Promise; -// formData(): Promise; // json(): Promise; -// text(): Promise; interface BroadcastChannelEventMap { message: MessageEvent; diff --git a/generated/lib.es2015.promise.d.ts b/generated/lib.es2015.promise.d.ts index 2986e65..dee6155 100644 --- a/generated/lib.es2015.promise.d.ts +++ b/generated/lib.es2015.promise.d.ts @@ -13,20 +13,8 @@ interface PromiseConstructor { new ( executor: ( resolve: undefined extends T - ? { - (value?: T | PromiseLike): void; - } - : { - (value: T | PromiseLike): void; - }, - // TODO: Revisit after https://github.com/microsoft/TypeScript/issues/42156 solves - // { - // (value: T | PromiseLike): void; - // } & (undefined extends T - // ? { - // (value?: T | PromiseLike): void; - // } - // : {}), + ? (value?: T | PromiseLike) => void + : (value: T | PromiseLike) => void, reject: (reason?: any) => void ) => void ): Promise; diff --git a/generated/lib.es2018.promise.d.ts b/generated/lib.es2018.promise.d.ts index 933058b..57cf2bd 100644 --- a/generated/lib.es2018.promise.d.ts +++ b/generated/lib.es2018.promise.d.ts @@ -9,5 +9,14 @@ interface Promise { * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). * @returns A Promise for the completion of the callback. */ - finally(onfinally?: (() => void) | undefined | null): Promise; + finally( + onfinally?: (() => U | PromiseLike) | null | undefined + ): Promise; } +// /** +// * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The +// * resolved value cannot be modified from the callback. +// * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). +// * @returns A Promise for the completion of the callback. +// */ +// finally(onfinally?: (() => void) | undefined | null): Promise diff --git a/generated/lib.es2020.promise.d.ts b/generated/lib.es2020.promise.d.ts index f97a558..3d450a9 100644 --- a/generated/lib.es2020.promise.d.ts +++ b/generated/lib.es2020.promise.d.ts @@ -6,8 +6,9 @@ interface PromiseFulfilledResult { interface PromiseRejectedResult { status: "rejected"; - reason: any; + reason: unknown; } +// reason: any; type PromiseSettledResult = | PromiseFulfilledResult @@ -22,7 +23,9 @@ interface PromiseConstructor { */ allSettled( values: T - ): Promise<{ -readonly [P in keyof T]: PromiseSettledResult> }>; + ): Promise<{ + -readonly [P in keyof T]: PromiseSettledResult>; + }>; /** * Creates a Promise that is resolved with an array of results when all @@ -31,6 +34,20 @@ interface PromiseConstructor { * @returns A new Promise. */ allSettled( - values: Iterable> + values: Iterable ): Promise>[]>; } +// /** +// * Creates a Promise that is resolved with an array of results when all +// * of the provided Promises resolve or reject. +// * @param values An array of Promises. +// * @returns A new Promise. +// */ +// allSettled(values: T): Promise<{ -readonly [P in keyof T]: PromiseSettledResult> }>; +// /** +// * Creates a Promise that is resolved with an array of results when all +// * of the provided Promises resolve or reject. +// * @param values An array of Promises. +// * @returns A new Promise. +// */ +// allSettled(values: Iterable>): Promise>[]>; diff --git a/generated/lib.es2021.promise.d.ts b/generated/lib.es2021.promise.d.ts index 8d26a84..6f0a1d2 100644 --- a/generated/lib.es2021.promise.d.ts +++ b/generated/lib.es2021.promise.d.ts @@ -30,5 +30,17 @@ interface PromiseConstructor { * @param values An array or iterable of Promises. * @returns A new Promise. */ - any(values: Iterable>): Promise>; + any(values: Iterable): Promise>; } +// /** +// * The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm. +// * @param values An array or iterable of Promises. +// * @returns A new Promise. +// */ +// any(values: T): Promise>; +// /** +// * The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm. +// * @param values An array or iterable of Promises. +// * @returns A new Promise. +// */ +// any(values: Iterable>): Promise> diff --git a/generated/lib.es5.d.ts b/generated/lib.es5.d.ts index 82306b5..a87eecf 100644 --- a/generated/lib.es5.d.ts +++ b/generated/lib.es5.d.ts @@ -2054,12 +2054,8 @@ interface TypedPropertyDescriptor { declare type PromiseConstructorLike = new ( executor: ( resolve: undefined extends T - ? { - (value?: T | PromiseLike): void; - } - : { - (value: T | PromiseLike): void; - }, + ? (value?: T | PromiseLike) => void + : (value: T | PromiseLike) => void, reject: (reason?: any) => void ) => void ) => PromiseLike; @@ -2072,51 +2068,80 @@ interface PromiseLike { * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of which ever callback is executed. */ - then( - onfulfilled?: - | ((value: T) => TResult1 | PromiseLike) - | undefined - | null, - onrejected?: - | ((reason: any) => TResult2 | PromiseLike) - | undefined - | null - ): PromiseLike; + then( + onfulfilled?: null | undefined, + onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined + ): PromiseLike; + + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined + ): PromiseLike; } +// /** +// * Attaches callbacks for the resolution and/or rejection of the Promise. +// * @param onfulfilled The callback to execute when the Promise is resolved. +// * @param onrejected The callback to execute when the Promise is rejected. +// * @returns A Promise for the completion of which ever callback is executed. +// */ +// then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): PromiseLike; -/** - * Represents the completion of an asynchronous operation - */ -interface Promise { +interface Promise extends PromiseLike { /** * Attaches callbacks for the resolution and/or rejection of the Promise. * @param onfulfilled The callback to execute when the Promise is resolved. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of which ever callback is executed. */ - then( - onfulfilled?: - | ((value: T) => TResult1 | PromiseLike) - | undefined - | null, - onrejected?: - | ((reason: any) => TResult2 | PromiseLike) - | undefined - | null - ): Promise; + then( + onfulfilled?: null | undefined, + onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined + ): Promise; + + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined + ): Promise; /** * Attaches a callback for only the rejection of the Promise. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of the callback. */ - catch( - onrejected?: - | ((reason: any) => TResult | PromiseLike) - | undefined - | null - ): Promise; + catch( + onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined + ): Promise; } +// /** +// * Represents the completion of an asynchronous operation +// */ +// interface Promise { +// /** +// * Attaches callbacks for the resolution and/or rejection of the Promise. +// * @param onfulfilled The callback to execute when the Promise is resolved. +// * @param onrejected The callback to execute when the Promise is rejected. +// * @returns A Promise for the completion of which ever callback is executed. +// */ +// then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Promise; +// /** +// * Attaches a callback for only the rejection of the Promise. +// * @param onrejected The callback to execute when the Promise is rejected. +// * @returns A Promise for the completion of the callback. +// */ +// catch(onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise; +// } /** * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`. diff --git a/lib/lib.dom.d.ts b/lib/lib.dom.d.ts index 419c5ee..fd187d8 100644 --- a/lib/lib.dom.d.ts +++ b/lib/lib.dom.d.ts @@ -1,9 +1,3 @@ interface Body { - readonly body: ReadableStream | null; - readonly bodyUsed: boolean; - arrayBuffer(): Promise; - blob(): Promise; - formData(): Promise; json(): Promise; - text(): Promise; } diff --git a/lib/lib.es2015.promise.d.ts b/lib/lib.es2015.promise.d.ts index ff8b87c..6c412f6 100644 --- a/lib/lib.es2015.promise.d.ts +++ b/lib/lib.es2015.promise.d.ts @@ -8,20 +8,8 @@ interface PromiseConstructor { new ( executor: ( resolve: undefined extends T - ? { - (value?: T | PromiseLike): void; - } - : { - (value: T | PromiseLike): void; - }, - // TODO: Revisit after https://github.com/microsoft/TypeScript/issues/42156 solves - // { - // (value: T | PromiseLike): void; - // } & (undefined extends T - // ? { - // (value?: T | PromiseLike): void; - // } - // : {}), + ? (value?: T | PromiseLike) => void + : (value: T | PromiseLike) => void, reject: (reason?: any) => void ) => void ): Promise; diff --git a/lib/lib.es2018.promise.d.ts b/lib/lib.es2018.promise.d.ts new file mode 100644 index 0000000..b279d07 --- /dev/null +++ b/lib/lib.es2018.promise.d.ts @@ -0,0 +1,14 @@ +/** + * Represents the completion of an asynchronous operation + */ +interface Promise { + /** + * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The + * resolved value cannot be modified from the callback. + * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). + * @returns A Promise for the completion of the callback. + */ + finally( + onfinally?: (() => U | PromiseLike) | null | undefined + ): Promise; +} diff --git a/lib/lib.es2020.promise.d.ts b/lib/lib.es2020.promise.d.ts new file mode 100644 index 0000000..641402c --- /dev/null +++ b/lib/lib.es2020.promise.d.ts @@ -0,0 +1,25 @@ +interface PromiseRejectedResult { + reason: unknown; +} + +interface PromiseConstructor { + /** + * Creates a Promise that is resolved with an array of results when all + * of the provided Promises resolve or reject. + * @param values An array of Promises. + * @returns A new Promise. + */ + allSettled( + values: T + ): Promise<{ -readonly [P in keyof T]: PromiseSettledResult> }>; + + /** + * Creates a Promise that is resolved with an array of results when all + * of the provided Promises resolve or reject. + * @param values An array of Promises. + * @returns A new Promise. + */ + allSettled( + values: Iterable + ): Promise>[]>; +} diff --git a/lib/lib.es2021.promise.d.ts b/lib/lib.es2021.promise.d.ts index 9bdc94f..a42bd17 100644 --- a/lib/lib.es2021.promise.d.ts +++ b/lib/lib.es2021.promise.d.ts @@ -1,3 +1,24 @@ interface AggregateError extends Error { errors: unknown[]; } + +/** + * Represents the completion of an asynchronous operation + */ +interface PromiseConstructor { + /** + * The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm. + * @param values An array or iterable of Promises. + * @returns A new Promise. + */ + any( + values: T + ): Promise>; + + /** + * The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm. + * @param values An array or iterable of Promises. + * @returns A new Promise. + */ + any(values: Iterable): Promise>; +} diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 8640d66..85698fd 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -574,16 +574,69 @@ interface ArrayConstructor { declare type PromiseConstructorLike = new ( executor: ( resolve: undefined extends T - ? { - (value?: T | PromiseLike): void; - } - : { - (value: T | PromiseLike): void; - }, + ? (value?: T | PromiseLike) => void + : (value: T | PromiseLike) => void, reject: (reason?: any) => void ) => void ) => PromiseLike; +interface PromiseLike { + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ + then( + onfulfilled?: null | undefined, + onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined + ): PromiseLike; + + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined + ): PromiseLike; +} + +interface Promise extends PromiseLike { + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ + then( + onfulfilled?: null | undefined, + onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined + ): Promise; + + /** + * Attaches callbacks for the resolution and/or rejection of the Promise. + * @param onfulfilled The callback to execute when the Promise is resolved. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of which ever callback is executed. + */ + then( + onfulfilled: (value: T) => U | PromiseLike, + onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined + ): Promise; + + /** + * Attaches a callback for only the rejection of the Promise. + * @param onrejected The callback to execute when the Promise is rejected. + * @returns A Promise for the completion of the callback. + */ + catch( + onrejected?: ((reason: unknown) => T | PromiseLike) | null | undefined + ): Promise; +} + interface TypedNumberArray { /** * Determines whether all the members of an array satisfy the specified test. diff --git a/tests/src/es2015.promise.ts b/tests/src/es2015.promise.ts index 6fd0440..7cccd1b 100644 --- a/tests/src/es2015.promise.ts +++ b/tests/src/es2015.promise.ts @@ -7,6 +7,10 @@ new Promise((resolve) => { // @ts-expect-error resolve(); }); +new Promise((resolve) => { + resolve(123); + resolve(); +}); new Promise((resolve) => { resolve(); resolve(123); diff --git a/tests/src/es2018.promise.ts b/tests/src/es2018.promise.ts new file mode 100644 index 0000000..dca6dcf --- /dev/null +++ b/tests/src/es2018.promise.ts @@ -0,0 +1,16 @@ +export const test = (getPromise: () => Promise) => { + const start = Date.now(); + + getPromise().finally(() => { + const end = Date.now(); + return end - start; + }); + + getPromise().finally(() => { + const end = Date.now(); + return Promise.resolve(end - start); + }); + + // @ts-expect-error + getPromise().finally(() => "NaN"); +}; diff --git a/tests/src/es2020.promise.ts b/tests/src/es2020.promise.ts new file mode 100644 index 0000000..25ce51f --- /dev/null +++ b/tests/src/es2020.promise.ts @@ -0,0 +1,20 @@ +import { expectType } from "tsd"; + +const test = async ( + values: Iterable>> +) => { + const results = await Promise.allSettled(values); + + for (const result of results) { + switch (result.status) { + case "fulfilled": + expectType(result.value); + break; + case "rejected": + expectType(result.reason); + break; + default: + expectType(result); + } + } +}; diff --git a/tests/src/es2021.promise.ts b/tests/src/es2021.promise.ts new file mode 100644 index 0000000..683ec13 --- /dev/null +++ b/tests/src/es2021.promise.ts @@ -0,0 +1,8 @@ +import { expectType } from "tsd"; + +const test = async ( + values: Iterable>> +) => { + const result = await Promise.any(values); + expectType(result); +}; diff --git a/tests/src/es5.ts b/tests/src/es5.ts index f72b5f9..07ccd78 100644 --- a/tests/src/es5.ts +++ b/tests/src/es5.ts @@ -21,6 +21,68 @@ const isEntries = (isKey: TypeGuard, isValue: TypeGuard) => isArray(isPair(isKey, isValue)); const isNumberStringEntries = isEntries(isNumber, isString); +// PromiseConstructorLike +const testPromiseConstructorLike = (MyPromise: PromiseConstructorLike) => { + new MyPromise((resolve) => { + resolve(123); + // @ts-expect-error + resolve(); + }); + new MyPromise((resolve) => { + resolve(123); + resolve(); + }); + new MyPromise((resolve) => { + resolve(); + resolve(123); + }); +}; +// Promise +const testPromise = (promise: Promise) => { + expectType>(promise.then()); + expectType>(promise.catch()); + expectType>(promise.then(null)); + expectType>(promise.then(undefined)); + expectType>(promise.catch(null)); + expectType>(promise.catch(undefined)); + expectType>(promise.then(null, null)); + expectType>(promise.then(null, undefined)); + expectType>(promise.then(undefined, null)); + expectType>(promise.then(undefined, undefined)); + expectType>(promise.then(null, (err) => `${err}`)); + expectType>(promise.then(undefined, (err) => `${err}`)); + expectType>(promise.catch((err) => `${err}`)); + expectType>(promise.then((str) => str.length)); + expectType>(promise.then((str) => str.length, null)); + expectType>(promise.then((str) => str.length, undefined)); + expectType>( + promise.then((str) => Promise.resolve(str.length)) + ); + expectType>( + promise.then( + (str) => str.length, + (err) => `${err}`.length + ) + ); + expectType>( + promise.then( + (str) => str.length, + (err) => Promise.resolve(`${err}`.length) + ) + ); + // @ts-expect-error + promise.then((str: string) => str); + promise.then( + (str: string) => str.length, + // @ts-expect-error + () => "NaN" + ); + // @ts-expect-error + promise.then(null, (err) => `${err}`.length); + // @ts-expect-error + promise.catch(null, (err) => `${err}`.length); +}; + // eval expectType(eval("foo")); // Object