Skip to content

Commit 26a08a1

Browse files
authored
Merge pull request #28 from aaditmshah/feature/promise
💥 Add better type definitions for promises
2 parents 732ece6 + cd1a27e commit 26a08a1

23 files changed

+500
-109
lines changed

docs/diff.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ The following files are improved in better-typescript-lib:
1616
- [es2017.object.d.ts](./diff/es2017.object.d.ts.md)
1717
- [es2018.asyncgenerator.d.ts](./diff/es2018.asyncgenerator.d.ts.md)
1818
- [es2018.asynciterable.d.ts](./diff/es2018.asynciterable.d.ts.md)
19+
- [es2018.promise.d.ts](./diff/es2018.promise.d.ts.md)
1920
- [es2019.object.d.ts](./diff/es2019.object.d.ts.md)
2021
- [es2020.bigint.d.ts](./diff/es2020.bigint.d.ts.md)
22+
- [es2020.promise.d.ts](./diff/es2020.promise.d.ts.md)
2123
- [es2021.string.d.ts](./diff/es2021.string.d.ts.md)
2224
- [es2021.promise.d.ts](./diff/es2021.promise.d.ts.md)
2325
- [es2022.object.d.ts](./diff/es2022.object.d.ts.md)

docs/diff/es2015.promise.d.ts.md

+3-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Index: es2015.promise.d.ts
55
===================================================================
66
--- es2015.promise.d.ts
77
+++ es2015.promise.d.ts
8-
@@ -1,19 +1,32 @@
8+
@@ -1,19 +1,20 @@
99
interface PromiseConstructor {
1010
/**
1111
* A reference to the prototype.
@@ -23,20 +23,8 @@ Index: es2015.promise.d.ts
2323
executor: (
2424
- resolve: (value: T | PromiseLike<T>) => void,
2525
+ resolve: undefined extends T
26-
+ ? {
27-
+ (value?: T | PromiseLike<T>): void;
28-
+ }
29-
+ : {
30-
+ (value: T | PromiseLike<T>): void;
31-
+ },
32-
+ // TODO: Revisit after https://github.com/microsoft/TypeScript/issues/42156 solves
33-
+ // {
34-
+ // (value: T | PromiseLike<T>): void;
35-
+ // } & (undefined extends T
36-
+ // ? {
37-
+ // (value?: T | PromiseLike<T>): void;
38-
+ // }
39-
+ // : {}),
26+
+ ? (value?: T | PromiseLike<T>) => void
27+
+ : (value: T | PromiseLike<T>) => void,
4028
reject: (reason?: any) => void
4129
) => void
4230
): Promise<T>;

docs/diff/es2018.promise.d.ts.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# es2018.promise.d.ts Diffs
2+
3+
```diff
4+
Index: es2018.promise.d.ts
5+
===================================================================
6+
--- es2018.promise.d.ts
7+
+++ es2018.promise.d.ts
8+
@@ -7,6 +7,8 @@
9+
* resolved value cannot be modified from the callback.
10+
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
11+
* @returns A Promise for the completion of the callback.
12+
*/
13+
- finally(onfinally?: (() => void) | undefined | null): Promise<T>;
14+
+ finally<U>(
15+
+ onfinally?: (() => U | PromiseLike<U>) | null | undefined
16+
+ ): Promise<T>;
17+
}
18+
19+
```

docs/diff/es2020.promise.d.ts.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# es2020.promise.d.ts Diffs
2+
3+
```diff
4+
Index: es2020.promise.d.ts
5+
===================================================================
6+
--- es2020.promise.d.ts
7+
+++ es2020.promise.d.ts
8+
@@ -4,9 +4,9 @@
9+
}
10+
11+
interface PromiseRejectedResult {
12+
status: "rejected";
13+
- reason: any;
14+
+ reason: unknown;
15+
}
16+
17+
type PromiseSettledResult<T> =
18+
| PromiseFulfilledResult<T>
19+
@@ -20,16 +20,18 @@
20+
* @returns A new Promise.
21+
*/
22+
allSettled<T extends readonly unknown[] | []>(
23+
values: T
24+
- ): Promise<{ -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>> }>;
25+
+ ): Promise<{
26+
+ -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>;
27+
+ }>;
28+
29+
/**
30+
* Creates a Promise that is resolved with an array of results when all
31+
* of the provided Promises resolve or reject.
32+
* @param values An array of Promises.
33+
* @returns A new Promise.
34+
*/
35+
allSettled<T>(
36+
- values: Iterable<T | PromiseLike<T>>
37+
+ values: Iterable<T>
38+
): Promise<PromiseSettledResult<Awaited<T>>[]>;
39+
}
40+
41+
```

docs/diff/es2021.promise.d.ts.md

+8
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,13 @@ Index: es2021.promise.d.ts
1313

1414
interface AggregateErrorConstructor {
1515
new (errors: Iterable<any>, message?: string): AggregateError;
16+
@@ -27,6 +27,6 @@
17+
* 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.
18+
* @param values An array or iterable of Promises.
19+
* @returns A new Promise.
20+
*/
21+
- any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>;
22+
+ any<T>(values: Iterable<T>): Promise<Awaited<T>>;
23+
}
1624

1725
```

docs/diff/es5.d.ts.md

+90-7
Original file line numberDiff line numberDiff line change
@@ -829,23 +829,106 @@ Index: es5.d.ts
829829

830830
declare var Array: ArrayConstructor;
831831

832-
@@ -1737,9 +1776,15 @@
832+
@@ -1737,9 +1776,11 @@
833833
}
834834

835835
declare type PromiseConstructorLike = new <T>(
836836
executor: (
837837
- resolve: (value: T | PromiseLike<T>) => void,
838838
+ resolve: undefined extends T
839-
+ ? {
840-
+ (value?: T | PromiseLike<T>): void;
841-
+ }
842-
+ : {
843-
+ (value: T | PromiseLike<T>): void;
844-
+ },
839+
+ ? (value?: T | PromiseLike<T>) => void
840+
+ : (value: T | PromiseLike<T>) => void,
845841
reject: (reason?: any) => void
846842
) => void
847843
) => PromiseLike<T>;
848844

845+
@@ -1749,52 +1790,56 @@
846+
* @param onfulfilled The callback to execute when the Promise is resolved.
847+
* @param onrejected The callback to execute when the Promise is rejected.
848+
* @returns A Promise for the completion of which ever callback is executed.
849+
*/
850+
- then<TResult1 = T, TResult2 = never>(
851+
- onfulfilled?:
852+
- | ((value: T) => TResult1 | PromiseLike<TResult1>)
853+
- | undefined
854+
- | null,
855+
- onrejected?:
856+
- | ((reason: any) => TResult2 | PromiseLike<TResult2>)
857+
- | undefined
858+
- | null
859+
- ): PromiseLike<TResult1 | TResult2>;
860+
+ then(
861+
+ onfulfilled?: null | undefined,
862+
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined
863+
+ ): PromiseLike<T>;
864+
+
865+
+ /**
866+
+ * Attaches callbacks for the resolution and/or rejection of the Promise.
867+
+ * @param onfulfilled The callback to execute when the Promise is resolved.
868+
+ * @param onrejected The callback to execute when the Promise is rejected.
869+
+ * @returns A Promise for the completion of which ever callback is executed.
870+
+ */
871+
+ then<U>(
872+
+ onfulfilled: (value: T) => U | PromiseLike<U>,
873+
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined
874+
+ ): PromiseLike<U>;
875+
}
876+
877+
-/**
878+
- * Represents the completion of an asynchronous operation
879+
- */
880+
-interface Promise<T> {
881+
+interface Promise<T> extends PromiseLike<T> {
882+
/**
883+
* Attaches callbacks for the resolution and/or rejection of the Promise.
884+
* @param onfulfilled The callback to execute when the Promise is resolved.
885+
* @param onrejected The callback to execute when the Promise is rejected.
886+
* @returns A Promise for the completion of which ever callback is executed.
887+
*/
888+
- then<TResult1 = T, TResult2 = never>(
889+
- onfulfilled?:
890+
- | ((value: T) => TResult1 | PromiseLike<TResult1>)
891+
- | undefined
892+
- | null,
893+
- onrejected?:
894+
- | ((reason: any) => TResult2 | PromiseLike<TResult2>)
895+
- | undefined
896+
- | null
897+
- ): Promise<TResult1 | TResult2>;
898+
+ then(
899+
+ onfulfilled?: null | undefined,
900+
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined
901+
+ ): Promise<T>;
902+
903+
/**
904+
+ * Attaches callbacks for the resolution and/or rejection of the Promise.
905+
+ * @param onfulfilled The callback to execute when the Promise is resolved.
906+
+ * @param onrejected The callback to execute when the Promise is rejected.
907+
+ * @returns A Promise for the completion of which ever callback is executed.
908+
+ */
909+
+ then<U>(
910+
+ onfulfilled: (value: T) => U | PromiseLike<U>,
911+
+ onrejected?: ((reason: unknown) => U | PromiseLike<U>) | null | undefined
912+
+ ): Promise<U>;
913+
+
914+
+ /**
915+
* Attaches a callback for only the rejection of the Promise.
916+
* @param onrejected The callback to execute when the Promise is rejected.
917+
* @returns A Promise for the completion of the callback.
918+
*/
919+
- catch<TResult = never>(
920+
- onrejected?:
921+
- | ((reason: any) => TResult | PromiseLike<TResult>)
922+
- | undefined
923+
- | null
924+
- ): Promise<T | TResult>;
925+
+ catch(
926+
+ onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined
927+
+ ): Promise<T>;
928+
}
929+
930+
/**
931+
* Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`.
849932
@@ -2144,20 +2189,24 @@
850933
* is treated as length+end.
851934
* @param end If not specified, length of the this object is used as its default value.

generated/lib.dom.d.ts

-6
Original file line numberDiff line numberDiff line change
@@ -2826,13 +2826,7 @@ interface Body {
28262826
json(): Promise<JSONValue>;
28272827
text(): Promise<string>;
28282828
}
2829-
// readonly body: ReadableStream<Uint8Array> | null;
2830-
// readonly bodyUsed: boolean;
2831-
// arrayBuffer(): Promise<ArrayBuffer>;
2832-
// blob(): Promise<Blob>;
2833-
// formData(): Promise<FormData>;
28342829
// json(): Promise<any>;
2835-
// text(): Promise<string>;
28362830

28372831
interface BroadcastChannelEventMap {
28382832
message: MessageEvent;

generated/lib.es2015.promise.d.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,8 @@ interface PromiseConstructor {
1313
new <T>(
1414
executor: (
1515
resolve: undefined extends T
16-
? {
17-
(value?: T | PromiseLike<T>): void;
18-
}
19-
: {
20-
(value: T | PromiseLike<T>): void;
21-
},
22-
// TODO: Revisit after https://github.com/microsoft/TypeScript/issues/42156 solves
23-
// {
24-
// (value: T | PromiseLike<T>): void;
25-
// } & (undefined extends T
26-
// ? {
27-
// (value?: T | PromiseLike<T>): void;
28-
// }
29-
// : {}),
16+
? (value?: T | PromiseLike<T>) => void
17+
: (value: T | PromiseLike<T>) => void,
3018
reject: (reason?: any) => void
3119
) => void
3220
): Promise<T>;

generated/lib.es2018.promise.d.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,14 @@ interface Promise<T> {
99
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
1010
* @returns A Promise for the completion of the callback.
1111
*/
12-
finally(onfinally?: (() => void) | undefined | null): Promise<T>;
12+
finally<U>(
13+
onfinally?: (() => U | PromiseLike<U>) | null | undefined
14+
): Promise<T>;
1315
}
16+
// /**
17+
// * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The
18+
// * resolved value cannot be modified from the callback.
19+
// * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
20+
// * @returns A Promise for the completion of the callback.
21+
// */
22+
// finally(onfinally?: (() => void) | undefined | null): Promise<T>

generated/lib.es2020.promise.d.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ interface PromiseFulfilledResult<T> {
66

77
interface PromiseRejectedResult {
88
status: "rejected";
9-
reason: any;
9+
reason: unknown;
1010
}
11+
// reason: any;
1112

1213
type PromiseSettledResult<T> =
1314
| PromiseFulfilledResult<T>
@@ -22,7 +23,9 @@ interface PromiseConstructor {
2223
*/
2324
allSettled<T extends readonly unknown[] | []>(
2425
values: T
25-
): Promise<{ -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>> }>;
26+
): Promise<{
27+
-readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>;
28+
}>;
2629

2730
/**
2831
* Creates a Promise that is resolved with an array of results when all
@@ -31,6 +34,20 @@ interface PromiseConstructor {
3134
* @returns A new Promise.
3235
*/
3336
allSettled<T>(
34-
values: Iterable<T | PromiseLike<T>>
37+
values: Iterable<T>
3538
): Promise<PromiseSettledResult<Awaited<T>>[]>;
3639
}
40+
// /**
41+
// * Creates a Promise that is resolved with an array of results when all
42+
// * of the provided Promises resolve or reject.
43+
// * @param values An array of Promises.
44+
// * @returns A new Promise.
45+
// */
46+
// allSettled<T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>> }>;
47+
// /**
48+
// * Creates a Promise that is resolved with an array of results when all
49+
// * of the provided Promises resolve or reject.
50+
// * @param values An array of Promises.
51+
// * @returns A new Promise.
52+
// */
53+
// allSettled<T>(values: Iterable<T | PromiseLike<T>>): Promise<PromiseSettledResult<Awaited<T>>[]>;

generated/lib.es2021.promise.d.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,17 @@ interface PromiseConstructor {
3030
* @param values An array or iterable of Promises.
3131
* @returns A new Promise.
3232
*/
33-
any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>;
33+
any<T>(values: Iterable<T>): Promise<Awaited<T>>;
3434
}
35+
// /**
36+
// * 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.
37+
// * @param values An array or iterable of Promises.
38+
// * @returns A new Promise.
39+
// */
40+
// any<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>;
41+
// /**
42+
// * 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.
43+
// * @param values An array or iterable of Promises.
44+
// * @returns A new Promise.
45+
// */
46+
// any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>

0 commit comments

Comments
 (0)