Skip to content

Commit 91ac4b2

Browse files
committed
Report a specialized error message for missing Promise constructor declaration when Promise type is available
1 parent ab053bf commit 91ac4b2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14299,6 +14299,9 @@ namespace ts {
1429914299
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);
1430014300
return unknownType;
1430114301
}
14302+
else if (!getGlobalPromiseConstructorSymbol()) {
14303+
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);
14304+
}
1430214305

1430314306
return promiseType;
1430414307
}
@@ -16895,7 +16898,12 @@ namespace ts {
1689516898
const promiseConstructorSymbol = resolveEntityName(promiseConstructorName, SymbolFlags.Value, /*ignoreErrors*/ true);
1689616899
const promiseConstructorType = promiseConstructorSymbol ? getTypeOfSymbol(promiseConstructorSymbol) : unknownType;
1689716900
if (promiseConstructorType === unknownType) {
16898-
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));
16901+
if (promiseConstructorName.kind === SyntaxKind.Identifier && promiseConstructorName.text === "Promise" && getTargetType(returnType) === tryGetGlobalPromiseType()) {
16902+
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);
16903+
}
16904+
else {
16905+
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));
16906+
}
1689916907
return unknownType;
1690016908
}
1690116909

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
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",

0 commit comments

Comments
 (0)