Skip to content

Commit c547f52

Browse files
authored
Merge pull request #14053 from Microsoft/usePromise
Move `Promise<T>` declaration to `lib.es5.d.ts`
2 parents 65d637d + 143edff commit c547f52

File tree

128 files changed

+824
-814
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+824
-814
lines changed

src/compiler/checker.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -14358,6 +14358,9 @@ namespace ts {
1435814358
error(func, Diagnostics.An_async_function_or_method_must_return_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option);
1435914359
return unknownType;
1436014360
}
14361+
else if (!getGlobalPromiseConstructorSymbol()) {
14362+
error(func, Diagnostics.An_async_function_or_method_in_ES5_SlashES3_requires_the_Promise_constructor_Make_sure_you_have_a_declaration_for_the_Promise_constructor_or_include_ES2015_in_your_lib_option);
14363+
}
1436114364

1436214365
return promiseType;
1436314366
}
@@ -16954,7 +16957,12 @@ namespace ts {
1695416957
const promiseConstructorSymbol = resolveEntityName(promiseConstructorName, SymbolFlags.Value, /*ignoreErrors*/ true);
1695516958
const promiseConstructorType = promiseConstructorSymbol ? getTypeOfSymbol(promiseConstructorSymbol) : unknownType;
1695616959
if (promiseConstructorType === unknownType) {
16957-
error(node.type, Diagnostics.Type_0_is_not_a_valid_async_function_return_type_in_ES5_SlashES3_because_it_does_not_refer_to_a_Promise_compatible_constructor_value, entityNameToString(promiseConstructorName));
16960+
if (promiseConstructorName.kind === SyntaxKind.Identifier && promiseConstructorName.text === "Promise" && getTargetType(returnType) === tryGetGlobalPromiseType()) {
16961+
error(node.type, Diagnostics.An_async_function_or_method_in_ES5_SlashES3_requires_the_Promise_constructor_Make_sure_you_have_a_declaration_for_the_Promise_constructor_or_include_ES2015_in_your_lib_option);
16962+
}
16963+
else {
16964+
error(node.type, Diagnostics.Type_0_is_not_a_valid_async_function_return_type_in_ES5_SlashES3_because_it_does_not_refer_to_a_Promise_compatible_constructor_value, entityNameToString(promiseConstructorName));
16965+
}
1695816966
return unknownType;
1695916967
}
1696016968

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,10 @@
20552055
"category": "Error",
20562056
"code": 2704
20572057
},
2058+
"An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.": {
2059+
"category": "Error",
2060+
"code": 2705
2061+
},
20582062

20592063
"Import declaration '{0}' is using private name '{1}'.": {
20602064
"category": "Error",

src/lib/es2015.promise.d.ts

-50
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,3 @@
1-
/**
2-
* Represents the completion of an asynchronous operation
3-
*/
4-
interface Promise<T> {
5-
/**
6-
* Attaches callbacks for the resolution and/or rejection of the Promise.
7-
* @param onfulfilled The callback to execute when the Promise is resolved.
8-
* @param onrejected The callback to execute when the Promise is rejected.
9-
* @returns A Promise for the completion of which ever callback is executed.
10-
*/
11-
then(onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null, onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): Promise<T>;
12-
13-
/**
14-
* Attaches callbacks for the resolution and/or rejection of the Promise.
15-
* @param onfulfilled The callback to execute when the Promise is resolved.
16-
* @param onrejected The callback to execute when the Promise is rejected.
17-
* @returns A Promise for the completion of which ever callback is executed.
18-
*/
19-
then<TResult>(onfulfilled: ((value: T) => T | PromiseLike<T>) | undefined | null, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
20-
21-
/**
22-
* Attaches callbacks for the resolution and/or rejection of the Promise.
23-
* @param onfulfilled The callback to execute when the Promise is resolved.
24-
* @param onrejected The callback to execute when the Promise is rejected.
25-
* @returns A Promise for the completion of which ever callback is executed.
26-
*/
27-
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>, onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<TResult>;
28-
29-
/**
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.
34-
*/
35-
then<TResult1, TResult2>(onfulfilled: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
36-
37-
/**
38-
* Attaches a callback for only the rejection of the Promise.
39-
* @param onrejected The callback to execute when the Promise is rejected.
40-
* @returns A Promise for the completion of the callback.
41-
*/
42-
catch(onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): Promise<T>;
43-
44-
/**
45-
* Attaches a callback for only the rejection of the Promise.
46-
* @param onrejected The callback to execute when the Promise is rejected.
47-
* @returns A Promise for the completion of the callback.
48-
*/
49-
catch<TResult>(onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
50-
}
511

522
interface PromiseConstructor {
533
/**

src/lib/es5.d.ts

+51
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,57 @@ interface PromiseLike<T> {
13511351
onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): PromiseLike<TResult1 | TResult2>;
13521352
}
13531353

1354+
/**
1355+
* Represents the completion of an asynchronous operation
1356+
*/
1357+
interface Promise<T> {
1358+
/**
1359+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1360+
* @param onfulfilled The callback to execute when the Promise is resolved.
1361+
* @param onrejected The callback to execute when the Promise is rejected.
1362+
* @returns A Promise for the completion of which ever callback is executed.
1363+
*/
1364+
then(onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null, onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): Promise<T>;
1365+
1366+
/**
1367+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1368+
* @param onfulfilled The callback to execute when the Promise is resolved.
1369+
* @param onrejected The callback to execute when the Promise is rejected.
1370+
* @returns A Promise for the completion of which ever callback is executed.
1371+
*/
1372+
then<TResult>(onfulfilled: ((value: T) => T | PromiseLike<T>) | undefined | null, onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
1373+
1374+
/**
1375+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1376+
* @param onfulfilled The callback to execute when the Promise is resolved.
1377+
* @param onrejected The callback to execute when the Promise is rejected.
1378+
* @returns A Promise for the completion of which ever callback is executed.
1379+
*/
1380+
then<TResult>(onfulfilled: (value: T) => TResult | PromiseLike<TResult>, onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<TResult>;
1381+
1382+
/**
1383+
* Attaches callbacks for the resolution and/or rejection of the Promise.
1384+
* @param onfulfilled The callback to execute when the Promise is resolved.
1385+
* @param onrejected The callback to execute when the Promise is rejected.
1386+
* @returns A Promise for the completion of which ever callback is executed.
1387+
*/
1388+
then<TResult1, TResult2>(onfulfilled: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
1389+
1390+
/**
1391+
* Attaches a callback for only the rejection of the Promise.
1392+
* @param onrejected The callback to execute when the Promise is rejected.
1393+
* @returns A Promise for the completion of the callback.
1394+
*/
1395+
catch(onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): Promise<T>;
1396+
1397+
/**
1398+
* Attaches a callback for only the rejection of the Promise.
1399+
* @param onrejected The callback to execute when the Promise is rejected.
1400+
* @returns A Promise for the completion of the callback.
1401+
*/
1402+
catch<TResult>(onrejected: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
1403+
}
1404+
13541405
interface ArrayLike<T> {
13551406
readonly length: number;
13561407
readonly [n: number]: T;

tests/baselines/reference/asyncAliasReturnType_es6.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
type PromiseAlias<T> = Promise<T>;
33
>PromiseAlias : Symbol(PromiseAlias, Decl(asyncAliasReturnType_es6.ts, 0, 0))
44
>T : Symbol(T, Decl(asyncAliasReturnType_es6.ts, 0, 18))
5-
>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, --, --))
5+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
66
>T : Symbol(T, Decl(asyncAliasReturnType_es6.ts, 0, 18))
77

88
async function f(): PromiseAlias<void> {

tests/baselines/reference/asyncArrowFunction1_es2017.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
var foo = async (): Promise<void> => {
44
>foo : Symbol(foo, Decl(asyncArrowFunction1_es2017.ts, 1, 3))
5-
>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, --, --))
5+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
66

77
};

tests/baselines/reference/asyncArrowFunction1_es5.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
var foo = async (): Promise<void> => {
44
>foo : Symbol(foo, Decl(asyncArrowFunction1_es5.ts, 1, 3))
5-
>Promise : Symbol(Promise, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
5+
>Promise : Symbol(Promise, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
66

77
};

tests/baselines/reference/asyncArrowFunction1_es6.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
var foo = async (): Promise<void> => {
44
>foo : Symbol(foo, Decl(asyncArrowFunction1_es6.ts, 1, 3))
5-
>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, --, --))
5+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
66

77
};

tests/baselines/reference/asyncAwaitWithCapturedBlockScopeVar.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function fn3() {
6565

6666
async function fn4(): Promise<number> {
6767
>fn4 : Symbol(fn4, Decl(asyncAwaitWithCapturedBlockScopeVar.ts, 24, 1))
68-
>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, --, --))
68+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
6969

7070
let ar = [];
7171
>ar : Symbol(ar, Decl(asyncAwaitWithCapturedBlockScopeVar.ts, 27, 7))

tests/baselines/reference/asyncAwait_es2017.symbols

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
type MyPromise<T> = Promise<T>;
33
>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11))
44
>T : Symbol(T, Decl(asyncAwait_es2017.ts, 0, 15))
5-
>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, --, --))
5+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
66
>T : Symbol(T, Decl(asyncAwait_es2017.ts, 0, 15))
77

88
declare var MyPromise: typeof Promise;
99
>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11))
10-
>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, --, --))
10+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
1111

1212
declare var p: Promise<number>;
1313
>p : Symbol(p, Decl(asyncAwait_es2017.ts, 2, 11))
14-
>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, --, --))
14+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
1515

1616
declare var mp: MyPromise<number>;
1717
>mp : Symbol(mp, Decl(asyncAwait_es2017.ts, 3, 11))
@@ -22,7 +22,7 @@ async function f0() { }
2222

2323
async function f1(): Promise<void> { }
2424
>f1 : Symbol(f1, Decl(asyncAwait_es2017.ts, 5, 23))
25-
>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, --, --))
25+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
2626

2727
async function f3(): MyPromise<void> { }
2828
>f3 : Symbol(f3, Decl(asyncAwait_es2017.ts, 6, 38))
@@ -33,7 +33,7 @@ let f4 = async function() { }
3333

3434
let f5 = async function(): Promise<void> { }
3535
>f5 : Symbol(f5, Decl(asyncAwait_es2017.ts, 10, 3))
36-
>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, --, --))
36+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
3737

3838
let f6 = async function(): MyPromise<void> { }
3939
>f6 : Symbol(f6, Decl(asyncAwait_es2017.ts, 11, 3))
@@ -44,7 +44,7 @@ let f7 = async () => { };
4444

4545
let f8 = async (): Promise<void> => { };
4646
>f8 : Symbol(f8, Decl(asyncAwait_es2017.ts, 14, 3))
47-
>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, --, --))
47+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
4848

4949
let f9 = async (): MyPromise<void> => { };
5050
>f9 : Symbol(f9, Decl(asyncAwait_es2017.ts, 15, 3))
@@ -60,7 +60,7 @@ let f11 = async () => mp;
6060

6161
let f12 = async (): Promise<number> => mp;
6262
>f12 : Symbol(f12, Decl(asyncAwait_es2017.ts, 18, 3))
63-
>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, --, --))
63+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
6464
>mp : Symbol(mp, Decl(asyncAwait_es2017.ts, 3, 11))
6565

6666
let f13 = async (): MyPromise<number> => p;
@@ -76,7 +76,7 @@ let o = {
7676

7777
async m2(): Promise<void> { },
7878
>m2 : Symbol(m2, Decl(asyncAwait_es2017.ts, 22, 16))
79-
>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, --, --))
79+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
8080

8181
async m3(): MyPromise<void> { }
8282
>m3 : Symbol(m3, Decl(asyncAwait_es2017.ts, 23, 31))
@@ -92,7 +92,7 @@ class C {
9292

9393
async m2(): Promise<void> { }
9494
>m2 : Symbol(C.m2, Decl(asyncAwait_es2017.ts, 28, 15))
95-
>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, --, --))
95+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
9696

9797
async m3(): MyPromise<void> { }
9898
>m3 : Symbol(C.m3, Decl(asyncAwait_es2017.ts, 29, 30))
@@ -103,7 +103,7 @@ class C {
103103

104104
static async m5(): Promise<void> { }
105105
>m5 : Symbol(C.m5, Decl(asyncAwait_es2017.ts, 31, 22))
106-
>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, --, --))
106+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
107107

108108
static async m6(): MyPromise<void> { }
109109
>m6 : Symbol(C.m6, Decl(asyncAwait_es2017.ts, 32, 37))

0 commit comments

Comments
 (0)