Skip to content

Commit 3e1da93

Browse files
authored
Merge pull request #10448 from Microsoft/improvePromiseType
Improve Promise type definition
2 parents 4e46b97 + 9d4219a commit 3e1da93

15 files changed

+3374
-51
lines changed

src/lib/es2015.promise.d.ts

+91-8
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,45 @@ interface Promise<T> {
88
* @param onrejected The callback to execute when the Promise is rejected.
99
* @returns A Promise for the completion of which ever callback is executed.
1010
*/
11-
then<TResult1, TResult2>(onfulfilled: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
11+
then(onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null, onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): Promise<T>;
1212

1313
/**
1414
* Attaches callbacks for the resolution and/or rejection of the Promise.
1515
* @param onfulfilled The callback to execute when the Promise is resolved.
1616
* @param onrejected The callback to execute when the Promise is rejected.
1717
* @returns A Promise for the completion of which ever callback is executed.
1818
*/
19-
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
19+
then<TResult>(onfulfilled: ((value: T) => T | PromiseLike<T>) | undefined | null, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
2020

2121
/**
2222
* Attaches callbacks for the resolution and/or rejection of the Promise.
2323
* @param onfulfilled The callback to execute when the Promise is resolved.
24+
* @param onrejected The callback to execute when the Promise is rejected.
2425
* @returns A Promise for the completion of which ever callback is executed.
2526
*/
26-
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>): Promise<TResult>;
27+
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>, onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<TResult>;
2728

2829
/**
29-
* Creates a new Promise with the same internal state of this Promise.
30-
* @returns A Promise.
30+
* Attaches callbacks for the resolution and/or rejection of the Promise.
31+
* @param onfulfilled The callback to execute when the Promise is resolved.
32+
* @param onrejected The callback to execute when the Promise is rejected.
33+
* @returns A Promise for the completion of which ever callback is executed.
3134
*/
32-
then(): Promise<T>;
35+
then<TResult1, TResult2>(onfulfilled: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
3336

3437
/**
3538
* Attaches a callback for only the rejection of the Promise.
3639
* @param onrejected The callback to execute when the Promise is rejected.
3740
* @returns A Promise for the completion of the callback.
3841
*/
39-
catch<TResult>(onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
42+
catch(onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): Promise<T>;
4043

4144
/**
4245
* Attaches a callback for only the rejection of the Promise.
4346
* @param onrejected The callback to execute when the Promise is rejected.
4447
* @returns A Promise for the completion of the callback.
4548
*/
46-
catch(onrejected: (reason: any) => T | PromiseLike<T>): Promise<T>;
49+
catch<TResult>(onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
4750
}
4851

4952
interface PromiseConstructor {
@@ -140,6 +143,86 @@ interface PromiseConstructor {
140143
*/
141144
all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
142145

146+
/**
147+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
148+
* or rejected.
149+
* @param values An array of Promises.
150+
* @returns A new Promise.
151+
*/
152+
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>;
153+
154+
/**
155+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
156+
* or rejected.
157+
* @param values An array of Promises.
158+
* @returns A new Promise.
159+
*/
160+
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>;
161+
162+
/**
163+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
164+
* or rejected.
165+
* @param values An array of Promises.
166+
* @returns A new Promise.
167+
*/
168+
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>;
169+
170+
/**
171+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
172+
* or rejected.
173+
* @param values An array of Promises.
174+
* @returns A new Promise.
175+
*/
176+
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>;
177+
178+
/**
179+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
180+
* or rejected.
181+
* @param values An array of Promises.
182+
* @returns A new Promise.
183+
*/
184+
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>;
185+
186+
/**
187+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
188+
* or rejected.
189+
* @param values An array of Promises.
190+
* @returns A new Promise.
191+
*/
192+
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>;
193+
194+
/**
195+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
196+
* or rejected.
197+
* @param values An array of Promises.
198+
* @returns A new Promise.
199+
*/
200+
race<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<T1 | T2 | T3 | T4>;
201+
202+
/**
203+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
204+
* or rejected.
205+
* @param values An array of Promises.
206+
* @returns A new Promise.
207+
*/
208+
race<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<T1 | T2 | T3>;
209+
210+
/**
211+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
212+
* or rejected.
213+
* @param values An array of Promises.
214+
* @returns A new Promise.
215+
*/
216+
race<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<T1 | T2>;
217+
218+
/**
219+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
220+
* or rejected.
221+
* @param values An array of Promises.
222+
* @returns A new Promise.
223+
*/
224+
race<T>(values: (T | PromiseLike<T>)[]): Promise<T>;
225+
143226
/**
144227
* Creates a new rejected promise for the provided reason.
145228
* @param reason The reason the promise was rejected.

src/lib/es5.d.ts

+38-7
Original file line numberDiff line numberDiff line change
@@ -1271,13 +1271,44 @@ declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | P
12711271

12721272
interface PromiseLike<T> {
12731273
/**
1274-
* Attaches callbacks for the resolution and/or rejection of the Promise.
1275-
* @param onfulfilled The callback to execute when the Promise is resolved.
1276-
* @param onrejected The callback to execute when the Promise is rejected.
1277-
* @returns A Promise for the completion of which ever callback is executed.
1278-
*/
1279-
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): PromiseLike<TResult>;
1280-
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): PromiseLike<TResult>;
1274+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1275+
* @param onfulfilled The callback to execute when the Promise is resolved.
1276+
* @param onrejected The callback to execute when the Promise is rejected.
1277+
* @returns A Promise for the completion of which ever callback is executed.
1278+
*/
1279+
then(
1280+
onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null,
1281+
onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): PromiseLike<T>;
1282+
1283+
/**
1284+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1285+
* @param onfulfilled The callback to execute when the Promise is resolved.
1286+
* @param onrejected The callback to execute when the Promise is rejected.
1287+
* @returns A Promise for the completion of which ever callback is executed.
1288+
*/
1289+
then<TResult>(
1290+
onfulfilled: ((value: T) => T | PromiseLike<T>) | undefined | null,
1291+
onrejected: (reason: any) => TResult | PromiseLike<TResult>): PromiseLike<T | TResult>;
1292+
1293+
/**
1294+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1295+
* @param onfulfilled The callback to execute when the Promise is resolved.
1296+
* @param onrejected The callback to execute when the Promise is rejected.
1297+
* @returns A Promise for the completion of which ever callback is executed.
1298+
*/
1299+
then<TResult>(
1300+
onfulfilled: (value: T) => TResult | PromiseLike<TResult>,
1301+
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): PromiseLike<TResult>;
1302+
1303+
/**
1304+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1305+
* @param onfulfilled The callback to execute when the Promise is resolved.
1306+
* @param onrejected The callback to execute when the Promise is rejected.
1307+
* @returns A Promise for the completion of which ever callback is executed.
1308+
*/
1309+
then<TResult1, TResult2>(
1310+
onfulfilled: (value: T) => TResult1 | PromiseLike<TResult1>,
1311+
onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): PromiseLike<TResult1 | TResult2>;
12811312
}
12821313

12831314
interface ArrayLike<T> {

tests/baselines/reference/inferenceLimit.types

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class BrokenClass {
4646

4747
this.doStuff(order.id)
4848
>this.doStuff(order.id) .then((items) => { order.items = items; resolve(order); }) : Promise<void>
49-
>this.doStuff(order.id) .then : { <TResult1, TResult2>(onfulfilled: (value: void) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; <TResult>(onfulfilled: (value: void) => TResult | PromiseLike<TResult>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled: (value: void) => TResult | PromiseLike<TResult>): Promise<TResult>; (): Promise<void>; }
49+
>this.doStuff(order.id) .then : { (onfulfilled?: (value: void) => void | PromiseLike<void>, onrejected?: (reason: any) => void | PromiseLike<void>): Promise<void>; <TResult>(onfulfilled: (value: void) => void | PromiseLike<void>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<void | TResult>; <TResult>(onfulfilled: (value: void) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult1, TResult2>(onfulfilled: (value: void) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; }
5050
>this.doStuff(order.id) : Promise<void>
5151
>this.doStuff : (id: number) => Promise<void>
5252
>this : this
@@ -56,7 +56,7 @@ export class BrokenClass {
5656
>id : any
5757

5858
.then((items) => {
59-
>then : { <TResult1, TResult2>(onfulfilled: (value: void) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; <TResult>(onfulfilled: (value: void) => TResult | PromiseLike<TResult>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled: (value: void) => TResult | PromiseLike<TResult>): Promise<TResult>; (): Promise<void>; }
59+
>then : { (onfulfilled?: (value: void) => void | PromiseLike<void>, onrejected?: (reason: any) => void | PromiseLike<void>): Promise<void>; <TResult>(onfulfilled: (value: void) => void | PromiseLike<void>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<void | TResult>; <TResult>(onfulfilled: (value: void) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult1, TResult2>(onfulfilled: (value: void) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; }
6060
>(items) => { order.items = items; resolve(order); } : (items: void) => void
6161
>items : void
6262

@@ -78,7 +78,7 @@ export class BrokenClass {
7878

7979
return Promise.all(result.map(populateItems))
8080
>Promise.all(result.map(populateItems)) .then((orders: Array<MyModule.MyModel>) => { resolve(orders); }) : Promise<void>
81-
>Promise.all(result.map(populateItems)) .then : { <TResult1, TResult2>(onfulfilled: (value: {}[]) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; <TResult>(onfulfilled: (value: {}[]) => TResult | PromiseLike<TResult>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled: (value: {}[]) => TResult | PromiseLike<TResult>): Promise<TResult>; (): Promise<{}[]>; }
81+
>Promise.all(result.map(populateItems)) .then : { (onfulfilled?: (value: {}[]) => {}[] | PromiseLike<{}[]>, onrejected?: (reason: any) => {}[] | PromiseLike<{}[]>): Promise<{}[]>; <TResult>(onfulfilled: (value: {}[]) => {}[] | PromiseLike<{}[]>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<{}[] | TResult>; <TResult>(onfulfilled: (value: {}[]) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult1, TResult2>(onfulfilled: (value: {}[]) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; }
8282
>Promise.all(result.map(populateItems)) : Promise<{}[]>
8383
>Promise.all : { <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]>; <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]>; <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]>; <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]>; <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]>; <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]>; <T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<[T1, T2, T3, T4]>; <T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>; <T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>; <T>(values: (T | PromiseLike<T>)[]): Promise<T[]>; <TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>; }
8484
>Promise : PromiseConstructor
@@ -90,7 +90,7 @@ export class BrokenClass {
9090
>populateItems : (order: any) => Promise<{}>
9191

9292
.then((orders: Array<MyModule.MyModel>) => {
93-
>then : { <TResult1, TResult2>(onfulfilled: (value: {}[]) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; <TResult>(onfulfilled: (value: {}[]) => TResult | PromiseLike<TResult>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult>(onfulfilled: (value: {}[]) => TResult | PromiseLike<TResult>): Promise<TResult>; (): Promise<{}[]>; }
93+
>then : { (onfulfilled?: (value: {}[]) => {}[] | PromiseLike<{}[]>, onrejected?: (reason: any) => {}[] | PromiseLike<{}[]>): Promise<{}[]>; <TResult>(onfulfilled: (value: {}[]) => {}[] | PromiseLike<{}[]>, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<{}[] | TResult>; <TResult>(onfulfilled: (value: {}[]) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>; <TResult1, TResult2>(onfulfilled: (value: {}[]) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; }
9494
>(orders: Array<MyModule.MyModel>) => { resolve(orders); } : (orders: MyModule.MyModel[]) => void
9595
>orders : MyModule.MyModel[]
9696
>Array : T[]

0 commit comments

Comments
 (0)