Skip to content

Commit ab2750a

Browse files
committed
Improves Promise type definition.
Fixes #4903
1 parent d8ab098 commit ab2750a

19 files changed

+3394
-76
lines changed

src/lib/es2015.promise.d.ts

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,77 @@
22
* Represents the completion of an asynchronous operation
33
*/
44
interface Promise<T> {
5+
/**
6+
* Creates a new Promise with the same internal state of this Promise.
7+
* @returns A Promise.
8+
*/
9+
then(): Promise<T>;
10+
11+
/**
12+
* Attaches callbacks for the resolution and/or rejection of the Promise.
13+
* @param onfulfilled The callback to execute when the Promise is resolved.
14+
* @returns A Promise for the completion of which ever callback is executed.
15+
*/
16+
then(onfulfilled: undefined | null): Promise<T>;
17+
18+
/**
19+
* Attaches callbacks for the resolution and/or rejection of the Promise.
20+
* @param onfulfilled The callback to execute when the Promise is resolved.
21+
* @returns A Promise for the completion of which ever callback is executed.
22+
*/
23+
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>): Promise<TResult>;
24+
525
/**
626
* Attaches callbacks for the resolution and/or rejection of the Promise.
727
* @param onfulfilled The callback to execute when the Promise is resolved.
828
* @param onrejected The callback to execute when the Promise is rejected.
929
* @returns A Promise for the completion of which ever callback is executed.
1030
*/
11-
then<TResult1, TResult2>(onfulfilled: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
31+
then(onfulfilled: undefined | null, onrejected: undefined | null): Promise<T>;
32+
33+
/**
34+
* Attaches callbacks for the resolution and/or rejection of the Promise.
35+
* @param onfulfilled The callback to execute when the Promise is resolved.
36+
* @param onrejected The callback to execute when the Promise is rejected.
37+
* @returns A Promise for the completion of which ever callback is executed.
38+
*/
39+
then<TResult>(onfulfilled: undefined | null, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
1240

1341
/**
1442
* Attaches callbacks for the resolution and/or rejection of the Promise.
1543
* @param onfulfilled The callback to execute when the Promise is resolved.
1644
* @param onrejected The callback to execute when the Promise is rejected.
1745
* @returns A Promise for the completion of which ever callback is executed.
1846
*/
19-
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
47+
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>, onrejected: undefined | null): Promise<TResult>;
2048

2149
/**
2250
* Attaches callbacks for the resolution and/or rejection of the Promise.
2351
* @param onfulfilled The callback to execute when the Promise is resolved.
52+
* @param onrejected The callback to execute when the Promise is rejected.
2453
* @returns A Promise for the completion of which ever callback is executed.
2554
*/
26-
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>): Promise<TResult>;
55+
then<TResult1, TResult2>(onfulfilled: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
2756

2857
/**
2958
* Creates a new Promise with the same internal state of this Promise.
3059
* @returns A Promise.
3160
*/
32-
then(): Promise<T>;
61+
catch(): Promise<T>;
3362

3463
/**
3564
* Attaches a callback for only the rejection of the Promise.
3665
* @param onrejected The callback to execute when the Promise is rejected.
3766
* @returns A Promise for the completion of the callback.
3867
*/
39-
catch<TResult>(onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
68+
catch(onrejected: undefined | null): Promise<T>;
4069

4170
/**
4271
* Attaches a callback for only the rejection of the Promise.
4372
* @param onrejected The callback to execute when the Promise is rejected.
4473
* @returns A Promise for the completion of the callback.
4574
*/
46-
catch(onrejected: (reason: any) => T | PromiseLike<T>): Promise<T>;
75+
catch<TResult>(onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
4776
}
4877

4978
interface PromiseConstructor {
@@ -140,6 +169,86 @@ interface PromiseConstructor {
140169
*/
141170
all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
142171

172+
/**
173+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
174+
* or rejected.
175+
* @param values An array of Promises.
176+
* @returns A new Promise.
177+
*/
178+
race<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>;
179+
180+
/**
181+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
182+
* or rejected.
183+
* @param values An array of Promises.
184+
* @returns A new Promise.
185+
*/
186+
race<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
187+
188+
/**
189+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
190+
* or rejected.
191+
* @param values An array of Promises.
192+
* @returns A new Promise.
193+
*/
194+
race<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
195+
196+
/**
197+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
198+
* or rejected.
199+
* @param values An array of Promises.
200+
* @returns A new Promise.
201+
*/
202+
race<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
203+
204+
/**
205+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
206+
* or rejected.
207+
* @param values An array of Promises.
208+
* @returns A new Promise.
209+
*/
210+
race<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<T1 | T2 | T3 | T4 | T5 | T6>;
211+
212+
/**
213+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
214+
* or rejected.
215+
* @param values An array of Promises.
216+
* @returns A new Promise.
217+
*/
218+
race<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): Promise<T1 | T2 | T3 | T4 | T5>;
219+
220+
/**
221+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
222+
* or rejected.
223+
* @param values An array of Promises.
224+
* @returns A new Promise.
225+
*/
226+
race<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<T1 | T2 | T3 | T4>;
227+
228+
/**
229+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
230+
* or rejected.
231+
* @param values An array of Promises.
232+
* @returns A new Promise.
233+
*/
234+
race<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<T1 | T2 | T3>;
235+
236+
/**
237+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
238+
* or rejected.
239+
* @param values An array of Promises.
240+
* @returns A new Promise.
241+
*/
242+
race<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<T1 | T2>;
243+
244+
/**
245+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
246+
* or rejected.
247+
* @param values An array of Promises.
248+
* @returns A new Promise.
249+
*/
250+
race<T>(values: (T | PromiseLike<T>)[]): Promise<T>;
251+
143252
/**
144253
* Creates a new rejected promise for the provided reason.
145254
* @param reason The reason the promise was rejected.

tests/baselines/reference/inferenceLimit.symbols

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ export class BrokenClass {
3737
>reject : Symbol(reject, Decl(file1.ts, 13, 34))
3838

3939
this.doStuff(order.id)
40-
>this.doStuff(order.id) .then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
40+
>this.doStuff(order.id) .then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
4141
>this.doStuff : Symbol(BrokenClass.doStuff, Decl(file1.ts, 27, 3))
4242
>this : Symbol(BrokenClass, Decl(file1.ts, 1, 39))
4343
>doStuff : Symbol(BrokenClass.doStuff, Decl(file1.ts, 27, 3))
4444
>order : Symbol(order, Decl(file1.ts, 12, 25))
4545

4646
.then((items) => {
47-
>then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
47+
>then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
4848
>items : Symbol(items, Decl(file1.ts, 15, 17))
4949

5050
order.items = items;
@@ -60,7 +60,7 @@ export class BrokenClass {
6060
};
6161

6262
return Promise.all(result.map(populateItems))
63-
>Promise.all(result.map(populateItems)) .then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
63+
>Promise.all(result.map(populateItems)) .then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
6464
>Promise.all : Symbol(PromiseConstructor.all, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
6565
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
6666
>all : Symbol(PromiseConstructor.all, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
@@ -70,7 +70,7 @@ export class BrokenClass {
7070
>populateItems : Symbol(populateItems, Decl(file1.ts, 12, 7))
7171

7272
.then((orders: Array<MyModule.MyModel>) => {
73-
>then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
73+
>then : Symbol(Promise.then, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
7474
>orders : Symbol(orders, Decl(file1.ts, 23, 13))
7575
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
7676
>MyModule : Symbol(MyModule, Decl(file1.ts, 1, 6))

0 commit comments

Comments
 (0)