Skip to content

Commit da5fd93

Browse files
authored
Merge pull request #15350 from Microsoft/improve-message-for-untyped-module
Improve message for untyped module with noImplicitAny
2 parents ebfc911 + 3b8cafe commit da5fd93

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,10 +1641,14 @@ namespace ts {
16411641
error(errorNode, diag, moduleReference, resolvedModule.resolvedFileName);
16421642
}
16431643
else if (noImplicitAny && moduleNotFoundError) {
1644-
error(errorNode,
1644+
let errorInfo = chainDiagnosticMessages(/*details*/ undefined,
1645+
Diagnostics.Try_npm_install_types_Slash_0_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
1646+
moduleReference);
1647+
errorInfo = chainDiagnosticMessages(errorInfo,
16451648
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
16461649
moduleReference,
16471650
resolvedModule.resolvedFileName);
1651+
diagnostics.add(createDiagnosticForNodeFromMessageChain(errorNode, errorInfo));
16481652
}
16491653
// Failed imports and untyped modules are both treated in an untyped manner; only difference is whether we give a diagnostic first.
16501654
return undefined;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3310,6 +3310,10 @@
33103310
"category": "Error",
33113311
"code": 7034
33123312
},
3313+
"Try `npm install @types/{0}` if it exists or add a new declaration (.d.ts) file containing `declare module '{0}';`": {
3314+
"category": "Error",
3315+
"code": 7035
3316+
},
33133317
"You cannot rename this element.": {
33143318
"category": "Error",
33153319
"code": 8000

tests/baselines/reference/untypedModuleImport_noImplicitAny.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/a.ts(1,22): error TS7016: Could not find a declaration file for module 'foo'. '/node_modules/foo/index.js' implicitly has an 'any' type.
2+
Try `npm install @types/foo` if it exists or add a new declaration (.d.ts) file containing `declare module 'foo';`
23

34

45
==== /a.ts (1 errors) ====
56
import * as foo from "foo";
67
~~~~~
78
!!! error TS7016: Could not find a declaration file for module 'foo'. '/node_modules/foo/index.js' implicitly has an 'any' type.
9+
!!! error TS7016: Try `npm install @types/foo` if it exists or add a new declaration (.d.ts) file containing `declare module 'foo';`
810

911
==== /node_modules/foo/index.js (0 errors) ====
1012
// This tests that `--noImplicitAny` disables untyped modules.

0 commit comments

Comments
 (0)