From 1642d2afd85a3ef72dcf8051ff977be10601df9a Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Thu, 6 Apr 2023 13:30:39 +0530 Subject: [PATCH 1/4] :sparkles: Add better type definitions for promises --- docs/diff.md | 2 + docs/diff/es2015.promise.d.ts.md | 18 +-- docs/diff/es2018.promise.d.ts.md | 19 +++ docs/diff/es2020.promise.d.ts.md | 41 +++++ docs/diff/es2021.promise.d.ts.md | 8 + docs/diff/es5.d.ts.md | 240 +++++++++++++++++++----------- generated/lib.es2015.promise.d.ts | 16 +- generated/lib.es2018.promise.d.ts | 11 +- generated/lib.es2020.promise.d.ts | 23 ++- generated/lib.es2021.promise.d.ts | 14 +- generated/lib.es5.d.ts | 77 +++++----- lib/lib.es2015.promise.d.ts | 16 +- lib/lib.es2018.promise.d.ts | 14 ++ lib/lib.es2020.promise.d.ts | 25 ++++ lib/lib.es2021.promise.d.ts | 21 +++ lib/lib.es5.d.ts | 43 +++++- 16 files changed, 408 insertions(+), 180 deletions(-) create mode 100644 docs/diff/es2018.promise.d.ts.md create mode 100644 docs/diff/es2020.promise.d.ts.md create mode 100644 lib/lib.es2018.promise.d.ts create mode 100644 lib/lib.es2020.promise.d.ts 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..ae39be2 100644 --- a/docs/diff/es5.d.ts.md +++ b/docs/diff/es5.d.ts.md @@ -829,24 +829,86 @@ 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; -@@ -2144,20 +2189,24 @@ +@@ -1749,52 +1790,34 @@ + * @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; + +-/** +- * 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; ++ then( ++ onfulfilled: (value: T) => U | PromiseLike, ++ onrejected?: ((reason: unknown) => U | PromiseLike) | null | undefined ++ ): PromiseLike; ++} + ++interface Promise extends PromiseLike { + /** + * 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 +2167,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -875,7 +937,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -2167,21 +2216,24 @@ +@@ -2167,21 +2194,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -905,7 +967,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2189,13 +2241,12 @@ +@@ -2189,13 +2219,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -922,7 +984,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2203,23 +2254,22 @@ +@@ -2203,23 +2232,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -952,7 +1014,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -2247,50 +2297,40 @@ +@@ -2247,50 +2275,40 @@ /** * The length of the array. */ @@ -1016,7 +1078,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -2299,46 +2339,32 @@ +@@ -2299,46 +2317,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1070,7 +1132,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -2347,14 +2373,14 @@ +@@ -2347,14 +2351,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1087,7 +1149,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -2375,20 +2401,24 @@ +@@ -2375,20 +2379,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -1116,7 +1178,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -2443,25 +2473,23 @@ +@@ -2443,25 +2451,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -1145,7 +1207,7 @@ Index: es5.d.ts } declare var Int8Array: Int8ArrayConstructor; -@@ -2499,20 +2527,24 @@ +@@ -2499,20 +2505,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -1174,7 +1236,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -2522,21 +2554,24 @@ +@@ -2522,21 +2532,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -1204,7 +1266,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2544,13 +2579,12 @@ +@@ -2544,13 +2557,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1221,7 +1283,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2558,23 +2592,22 @@ +@@ -2558,23 +2570,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1251,7 +1313,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -2602,50 +2635,40 @@ +@@ -2602,50 +2613,40 @@ /** * The length of the array. */ @@ -1315,7 +1377,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -2654,46 +2677,32 @@ +@@ -2654,46 +2655,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1369,7 +1431,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -2702,14 +2711,14 @@ +@@ -2702,14 +2689,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1386,7 +1448,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -2730,20 +2739,24 @@ +@@ -2730,20 +2717,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -1415,7 +1477,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -2799,25 +2812,23 @@ +@@ -2799,25 +2790,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -1444,7 +1506,7 @@ Index: es5.d.ts } declare var Uint8Array: Uint8ArrayConstructor; -@@ -2855,24 +2866,24 @@ +@@ -2855,24 +2844,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -1474,7 +1536,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -2882,21 +2893,24 @@ +@@ -2882,21 +2871,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -1504,7 +1566,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2904,17 +2918,12 @@ +@@ -2904,17 +2896,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1525,7 +1587,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2922,31 +2931,22 @@ +@@ -2922,31 +2909,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1563,7 +1625,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -2974,54 +2974,40 @@ +@@ -2974,54 +2952,40 @@ /** * The length of the array. */ @@ -1627,7 +1689,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -3030,46 +3016,32 @@ +@@ -3030,46 +2994,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1681,7 +1743,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -3078,14 +3050,14 @@ +@@ -3078,14 +3028,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1698,7 +1760,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -3106,24 +3078,24 @@ +@@ -3106,24 +3056,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -1728,7 +1790,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -3179,25 +3151,23 @@ +@@ -3179,25 +3129,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -1757,7 +1819,7 @@ Index: es5.d.ts } declare var Uint8ClampedArray: Uint8ClampedArrayConstructor; -@@ -3235,20 +3205,24 @@ +@@ -3235,20 +3183,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -1786,7 +1848,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -3258,21 +3232,24 @@ +@@ -3258,21 +3210,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -1816,7 +1878,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3280,13 +3257,12 @@ +@@ -3280,13 +3235,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1833,7 +1895,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3294,23 +3270,22 @@ +@@ -3294,23 +3248,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1863,7 +1925,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. * @param searchElement The value to locate in the array. -@@ -3337,50 +3312,40 @@ +@@ -3337,50 +3290,40 @@ /** * The length of the array. */ @@ -1927,7 +1989,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -3389,46 +3354,32 @@ +@@ -3389,46 +3332,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1981,7 +2043,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -3437,14 +3388,14 @@ +@@ -3437,14 +3366,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1998,7 +2060,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -3465,20 +3416,24 @@ +@@ -3465,20 +3394,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2027,7 +2089,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -3534,25 +3489,23 @@ +@@ -3534,25 +3467,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -2056,7 +2118,7 @@ Index: es5.d.ts } declare var Int16Array: Int16ArrayConstructor; -@@ -3590,20 +3543,24 @@ +@@ -3590,20 +3521,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -2085,7 +2147,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -3613,21 +3570,24 @@ +@@ -3613,21 +3548,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -2115,7 +2177,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3635,13 +3595,12 @@ +@@ -3635,13 +3573,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2132,7 +2194,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3649,23 +3608,22 @@ +@@ -3649,23 +3586,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2162,7 +2224,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -3693,50 +3651,40 @@ +@@ -3693,50 +3629,40 @@ /** * The length of the array. */ @@ -2226,7 +2288,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -3745,46 +3693,32 @@ +@@ -3745,46 +3671,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2280,7 +2342,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -3793,14 +3727,14 @@ +@@ -3793,14 +3705,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2297,7 +2359,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -3821,20 +3755,24 @@ +@@ -3821,20 +3733,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2326,7 +2388,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -3890,25 +3828,23 @@ +@@ -3890,25 +3806,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -2355,7 +2417,7 @@ Index: es5.d.ts } declare var Uint16Array: Uint16ArrayConstructor; /** -@@ -3945,20 +3881,24 @@ +@@ -3945,20 +3859,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -2384,7 +2446,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -3968,21 +3908,24 @@ +@@ -3968,21 +3886,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -2414,7 +2476,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3990,13 +3933,12 @@ +@@ -3990,13 +3911,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2431,7 +2493,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4004,23 +3946,22 @@ +@@ -4004,23 +3924,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2461,7 +2523,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -4048,50 +3989,40 @@ +@@ -4048,50 +3967,40 @@ /** * The length of the array. */ @@ -2525,7 +2587,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -4100,46 +4031,32 @@ +@@ -4100,46 +4009,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2579,7 +2641,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -4148,14 +4065,14 @@ +@@ -4148,14 +4043,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2596,7 +2658,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -4176,20 +4093,24 @@ +@@ -4176,20 +4071,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2625,7 +2687,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -4245,25 +4166,23 @@ +@@ -4245,25 +4144,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -2654,7 +2716,7 @@ Index: es5.d.ts } declare var Int32Array: Int32ArrayConstructor; -@@ -4301,20 +4220,24 @@ +@@ -4301,20 +4198,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -2683,7 +2745,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -4324,21 +4247,24 @@ +@@ -4324,21 +4225,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -2713,7 +2775,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4346,13 +4272,12 @@ +@@ -4346,13 +4250,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2730,7 +2792,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4360,23 +4285,22 @@ +@@ -4360,23 +4263,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2760,7 +2822,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. * @param searchElement The value to locate in the array. -@@ -4403,50 +4327,40 @@ +@@ -4403,50 +4305,40 @@ /** * The length of the array. */ @@ -2824,7 +2886,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -4455,46 +4369,32 @@ +@@ -4455,46 +4347,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2878,7 +2940,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -4503,14 +4403,14 @@ +@@ -4503,14 +4381,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2895,7 +2957,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -4531,20 +4431,24 @@ +@@ -4531,20 +4409,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2924,7 +2986,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -4600,25 +4504,23 @@ +@@ -4600,25 +4482,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -2953,7 +3015,7 @@ Index: es5.d.ts } declare var Uint32Array: Uint32ArrayConstructor; -@@ -4656,20 +4558,24 @@ +@@ -4656,20 +4536,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -2982,7 +3044,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -4679,21 +4585,24 @@ +@@ -4679,21 +4563,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -3012,7 +3074,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4701,13 +4610,12 @@ +@@ -4701,13 +4588,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3029,7 +3091,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4715,23 +4623,22 @@ +@@ -4715,23 +4601,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3059,7 +3121,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -4759,50 +4666,40 @@ +@@ -4759,50 +4644,40 @@ /** * The length of the array. */ @@ -3123,7 +3185,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -4811,46 +4708,32 @@ +@@ -4811,46 +4686,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3177,7 +3239,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -4859,14 +4742,14 @@ +@@ -4859,14 +4720,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3194,7 +3256,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -4887,20 +4770,24 @@ +@@ -4887,20 +4748,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -3223,7 +3285,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -4956,25 +4843,23 @@ +@@ -4956,25 +4821,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -3252,7 +3314,7 @@ Index: es5.d.ts } declare var Float32Array: Float32ArrayConstructor; -@@ -5012,20 +4897,24 @@ +@@ -5012,20 +4875,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -3281,7 +3343,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -5035,21 +4924,24 @@ +@@ -5035,21 +4902,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -3311,7 +3373,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -5057,13 +4949,12 @@ +@@ -5057,13 +4927,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3328,7 +3390,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -5071,23 +4962,22 @@ +@@ -5071,23 +4940,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3358,7 +3420,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -5115,50 +5005,40 @@ +@@ -5115,50 +4983,40 @@ /** * The length of the array. */ @@ -3422,7 +3484,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -5167,46 +5047,32 @@ +@@ -5167,46 +5025,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3476,7 +3538,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -5215,14 +5081,14 @@ +@@ -5215,14 +5059,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3493,7 +3555,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -5243,20 +5109,24 @@ +@@ -5243,20 +5087,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -3522,7 +3584,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -5303,25 +5173,23 @@ +@@ -5303,25 +5151,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -3551,7 +3613,7 @@ Index: es5.d.ts } declare var Float64Array: Float64ArrayConstructor; -@@ -5521,4 +5389,33 @@ +@@ -5521,4 +5367,33 @@ locales?: string | string[], options?: Intl.DateTimeFormatOptions ): string; 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..b2c3456 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,58 @@ 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; -/** - * 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; + 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; +interface Promise extends PromiseLike { /** * 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.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..549dd79 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -574,16 +574,47 @@ 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 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. From ac27a5163132dcfddf3e95af8e3cc31efec345d0 Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Thu, 6 Apr 2023 13:32:13 +0530 Subject: [PATCH 2/4] :fire: Remove unmodified properties of `Body` --- generated/lib.dom.d.ts | 6 ------ lib/lib.dom.d.ts | 6 ------ 2 files changed, 12 deletions(-) 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/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; } From a287425fa640d741b77d7d3a47702e040a3bc41c Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Mon, 10 Apr 2023 14:34:06 +0530 Subject: [PATCH 3/4] :bug: `Promise#then` should return a `Promise` --- docs/diff/es5.d.ts.md | 199 +++++++++++++++++++++++------------------ generated/lib.es5.d.ts | 22 +++++ lib/lib.es5.d.ts | 22 +++++ 3 files changed, 154 insertions(+), 89 deletions(-) diff --git a/docs/diff/es5.d.ts.md b/docs/diff/es5.d.ts.md index ae39be2..33f6efd 100644 --- a/docs/diff/es5.d.ts.md +++ b/docs/diff/es5.d.ts.md @@ -842,7 +842,7 @@ Index: es5.d.ts ) => void ) => PromiseLike; -@@ -1749,52 +1790,34 @@ +@@ -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. @@ -857,16 +857,28 @@ Index: es5.d.ts - | 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. @@ -883,14 +895,23 @@ Index: es5.d.ts - | 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 -+ ): PromiseLike; -+} - -+interface Promise extends PromiseLike { - /** ++ ): 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. @@ -908,7 +929,7 @@ Index: es5.d.ts /** * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`. -@@ -2144,20 +2167,24 @@ +@@ -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. */ @@ -937,7 +958,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -2167,21 +2194,24 @@ +@@ -2167,21 +2216,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -967,7 +988,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2189,13 +2219,12 @@ +@@ -2189,13 +2241,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -984,7 +1005,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2203,23 +2232,22 @@ +@@ -2203,23 +2254,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1014,7 +1035,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -2247,50 +2275,40 @@ +@@ -2247,50 +2297,40 @@ /** * The length of the array. */ @@ -1078,7 +1099,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -2299,46 +2317,32 @@ +@@ -2299,46 +2339,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1132,7 +1153,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -2347,14 +2351,14 @@ +@@ -2347,14 +2373,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1149,7 +1170,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -2375,20 +2379,24 @@ +@@ -2375,20 +2401,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -1178,7 +1199,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -2443,25 +2451,23 @@ +@@ -2443,25 +2473,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -1207,7 +1228,7 @@ Index: es5.d.ts } declare var Int8Array: Int8ArrayConstructor; -@@ -2499,20 +2505,24 @@ +@@ -2499,20 +2527,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -1236,7 +1257,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -2522,21 +2532,24 @@ +@@ -2522,21 +2554,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -1266,7 +1287,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2544,13 +2557,12 @@ +@@ -2544,13 +2579,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1283,7 +1304,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2558,23 +2570,22 @@ +@@ -2558,23 +2592,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1313,7 +1334,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -2602,50 +2613,40 @@ +@@ -2602,50 +2635,40 @@ /** * The length of the array. */ @@ -1377,7 +1398,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -2654,46 +2655,32 @@ +@@ -2654,46 +2677,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1431,7 +1452,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -2702,14 +2689,14 @@ +@@ -2702,14 +2711,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1448,7 +1469,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -2730,20 +2717,24 @@ +@@ -2730,20 +2739,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -1477,7 +1498,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -2799,25 +2790,23 @@ +@@ -2799,25 +2812,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -1506,7 +1527,7 @@ Index: es5.d.ts } declare var Uint8Array: Uint8ArrayConstructor; -@@ -2855,24 +2844,24 @@ +@@ -2855,24 +2866,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -1536,7 +1557,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -2882,21 +2871,24 @@ +@@ -2882,21 +2893,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -1566,7 +1587,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2904,17 +2896,12 @@ +@@ -2904,17 +2918,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1587,7 +1608,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -2922,31 +2909,22 @@ +@@ -2922,31 +2931,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1625,7 +1646,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -2974,54 +2952,40 @@ +@@ -2974,54 +2974,40 @@ /** * The length of the array. */ @@ -1689,7 +1710,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -3030,46 +2994,32 @@ +@@ -3030,46 +3016,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1743,7 +1764,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -3078,14 +3028,14 @@ +@@ -3078,14 +3050,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -1760,7 +1781,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -3106,24 +3056,24 @@ +@@ -3106,24 +3078,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -1790,7 +1811,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -3179,25 +3129,23 @@ +@@ -3179,25 +3151,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -1819,7 +1840,7 @@ Index: es5.d.ts } declare var Uint8ClampedArray: Uint8ClampedArrayConstructor; -@@ -3235,20 +3183,24 @@ +@@ -3235,20 +3205,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -1848,7 +1869,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -3258,21 +3210,24 @@ +@@ -3258,21 +3232,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -1878,7 +1899,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3280,13 +3235,12 @@ +@@ -3280,13 +3257,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1895,7 +1916,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3294,23 +3248,22 @@ +@@ -3294,23 +3270,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -1925,7 +1946,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. * @param searchElement The value to locate in the array. -@@ -3337,50 +3290,40 @@ +@@ -3337,50 +3312,40 @@ /** * The length of the array. */ @@ -1989,7 +2010,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -3389,46 +3332,32 @@ +@@ -3389,46 +3354,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2043,7 +2064,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -3437,14 +3366,14 @@ +@@ -3437,14 +3388,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2060,7 +2081,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -3465,20 +3394,24 @@ +@@ -3465,20 +3416,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2089,7 +2110,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -3534,25 +3467,23 @@ +@@ -3534,25 +3489,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -2118,7 +2139,7 @@ Index: es5.d.ts } declare var Int16Array: Int16ArrayConstructor; -@@ -3590,20 +3521,24 @@ +@@ -3590,20 +3543,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -2147,7 +2168,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -3613,21 +3548,24 @@ +@@ -3613,21 +3570,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -2177,7 +2198,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3635,13 +3573,12 @@ +@@ -3635,13 +3595,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2194,7 +2215,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3649,23 +3586,22 @@ +@@ -3649,23 +3608,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2224,7 +2245,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -3693,50 +3629,40 @@ +@@ -3693,50 +3651,40 @@ /** * The length of the array. */ @@ -2288,7 +2309,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -3745,46 +3671,32 @@ +@@ -3745,46 +3693,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2342,7 +2363,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -3793,14 +3705,14 @@ +@@ -3793,14 +3727,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2359,7 +2380,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -3821,20 +3733,24 @@ +@@ -3821,20 +3755,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2388,7 +2409,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -3890,25 +3806,23 @@ +@@ -3890,25 +3828,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -2417,7 +2438,7 @@ Index: es5.d.ts } declare var Uint16Array: Uint16ArrayConstructor; /** -@@ -3945,20 +3859,24 @@ +@@ -3945,20 +3881,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -2446,7 +2467,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -3968,21 +3886,24 @@ +@@ -3968,21 +3908,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -2476,7 +2497,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -3990,13 +3911,12 @@ +@@ -3990,13 +3933,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2493,7 +2514,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4004,23 +3924,22 @@ +@@ -4004,23 +3946,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2523,7 +2544,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -4048,50 +3967,40 @@ +@@ -4048,50 +3989,40 @@ /** * The length of the array. */ @@ -2587,7 +2608,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -4100,46 +4009,32 @@ +@@ -4100,46 +4031,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2641,7 +2662,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -4148,14 +4043,14 @@ +@@ -4148,14 +4065,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2658,7 +2679,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -4176,20 +4071,24 @@ +@@ -4176,20 +4093,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2687,7 +2708,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -4245,25 +4144,23 @@ +@@ -4245,25 +4166,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -2716,7 +2737,7 @@ Index: es5.d.ts } declare var Int32Array: Int32ArrayConstructor; -@@ -4301,20 +4198,24 @@ +@@ -4301,20 +4220,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -2745,7 +2766,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -4324,21 +4225,24 @@ +@@ -4324,21 +4247,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -2775,7 +2796,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4346,13 +4250,12 @@ +@@ -4346,13 +4272,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2792,7 +2813,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4360,23 +4263,22 @@ +@@ -4360,23 +4285,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -2822,7 +2843,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. * @param searchElement The value to locate in the array. -@@ -4403,50 +4305,40 @@ +@@ -4403,50 +4327,40 @@ /** * The length of the array. */ @@ -2886,7 +2907,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -4455,46 +4347,32 @@ +@@ -4455,46 +4369,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2940,7 +2961,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -4503,14 +4381,14 @@ +@@ -4503,14 +4403,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -2957,7 +2978,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -4531,20 +4409,24 @@ +@@ -4531,20 +4431,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -2986,7 +3007,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -4600,25 +4482,23 @@ +@@ -4600,25 +4504,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -3015,7 +3036,7 @@ Index: es5.d.ts } declare var Uint32Array: Uint32ArrayConstructor; -@@ -4656,20 +4536,24 @@ +@@ -4656,20 +4558,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -3044,7 +3065,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -4679,21 +4563,24 @@ +@@ -4679,21 +4585,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -3074,7 +3095,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4701,13 +4588,12 @@ +@@ -4701,13 +4610,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3091,7 +3112,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -4715,23 +4601,22 @@ +@@ -4715,23 +4623,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3121,7 +3142,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -4759,50 +4644,40 @@ +@@ -4759,50 +4666,40 @@ /** * The length of the array. */ @@ -3185,7 +3206,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -4811,46 +4686,32 @@ +@@ -4811,46 +4708,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3239,7 +3260,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -4859,14 +4720,14 @@ +@@ -4859,14 +4742,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3256,7 +3277,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -4887,20 +4748,24 @@ +@@ -4887,20 +4770,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -3285,7 +3306,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -4956,25 +4821,23 @@ +@@ -4956,25 +4843,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -3314,7 +3335,7 @@ Index: es5.d.ts } declare var Float32Array: Float32ArrayConstructor; -@@ -5012,20 +4875,24 @@ +@@ -5012,20 +4897,24 @@ * is treated as length+end. * @param end If not specified, length of the this object is used as its default value. */ @@ -3343,7 +3364,7 @@ Index: es5.d.ts /** * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array -@@ -5035,21 +4902,24 @@ +@@ -5035,21 +4924,24 @@ * @param end index to stop filling the array at. If end is negative, it is treated as * length+end. */ @@ -3373,7 +3394,7 @@ Index: es5.d.ts * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -5057,13 +4927,12 @@ +@@ -5057,13 +4949,12 @@ * immediately returns that element value. Otherwise, find returns undefined. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3390,7 +3411,7 @@ Index: es5.d.ts * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * @param predicate find calls predicate once for each element of the array, in ascending -@@ -5071,23 +4940,22 @@ +@@ -5071,23 +4962,22 @@ * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. @@ -3420,7 +3441,7 @@ Index: es5.d.ts /** * Returns the index of the first occurrence of a value in an array. -@@ -5115,50 +4983,40 @@ +@@ -5115,50 +5005,40 @@ /** * The length of the array. */ @@ -3484,7 +3505,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array. The return value of * the callback function is the accumulated result, and is provided as an argument in the next * call to the callback function. -@@ -5167,46 +5025,32 @@ +@@ -5167,46 +5047,32 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3538,7 +3559,7 @@ Index: es5.d.ts * Calls the specified callback function for all the elements in an array, in descending order. * The return value of the callback function is the accumulated result, and is provided as an * argument in the next call to the callback function. -@@ -5215,14 +5059,14 @@ +@@ -5215,14 +5081,14 @@ * @param initialValue If initialValue is specified, it is used as the initial value to start * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. @@ -3555,7 +3576,7 @@ Index: es5.d.ts initialValue: U ): U; -@@ -5243,20 +5087,24 @@ +@@ -5243,20 +5109,24 @@ * @param start The beginning of the specified portion of the array. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. */ @@ -3584,7 +3605,7 @@ Index: es5.d.ts /** * Sorts an array. -@@ -5303,25 +5151,23 @@ +@@ -5303,25 +5173,23 @@ * Returns a new array from a set of elements. * @param items A set of elements to include in the new array object. */ @@ -3613,7 +3634,7 @@ Index: es5.d.ts } declare var Float64Array: Float64ArrayConstructor; -@@ -5521,4 +5367,33 @@ +@@ -5521,4 +5389,33 @@ locales?: string | string[], options?: Intl.DateTimeFormatOptions ): string; diff --git a/generated/lib.es5.d.ts b/generated/lib.es5.d.ts index b2c3456..a87eecf 100644 --- a/generated/lib.es5.d.ts +++ b/generated/lib.es5.d.ts @@ -2093,6 +2093,28 @@ interface PromiseLike { // then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): 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. diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 549dd79..85698fd 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -605,6 +605,28 @@ interface 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. From cd1a27e9fc8decd5713f03a1dcd93a8454d6a4d4 Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Mon, 10 Apr 2023 14:34:32 +0530 Subject: [PATCH 4/4] :white_check_mark: Add tests for promises --- tests/src/es2015.promise.ts | 4 +++ tests/src/es2018.promise.ts | 16 ++++++++++ tests/src/es2020.promise.ts | 20 ++++++++++++ tests/src/es2021.promise.ts | 8 +++++ tests/src/es5.ts | 62 +++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 tests/src/es2018.promise.ts create mode 100644 tests/src/es2020.promise.ts create mode 100644 tests/src/es2021.promise.ts 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