Skip to content

Commit ec37651

Browse files
author
Andy
authored
Use packageId for suggestion to install @types/packageName (#21241)
1 parent b0d7d5a commit ec37651

9 files changed

+63
-5
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,9 +2063,9 @@ namespace ts {
20632063
error(errorNode, diag, moduleReference, resolvedModule.resolvedFileName);
20642064
}
20652065
else if (noImplicitAny && moduleNotFoundError) {
2066-
let errorInfo = !resolvedModule.isExternalLibraryImport ? undefined : chainDiagnosticMessages(/*details*/ undefined,
2066+
let errorInfo = resolvedModule.packageId && chainDiagnosticMessages(/*details*/ undefined,
20672067
Diagnostics.Try_npm_install_types_Slash_0_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
2068-
moduleReference);
2068+
resolvedModule.packageId.name);
20692069
errorInfo = chainDiagnosticMessages(errorInfo,
20702070
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
20712071
moduleReference,

tests/baselines/reference/untypedModuleImport_noImplicitAny.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
!!! error TS7016: Could not find a declaration file for module 'foo'. '/node_modules/foo/index.js' implicitly has an 'any' type.
99
!!! error TS7016: Try `npm install @types/foo` if it exists or add a new declaration (.d.ts) file containing `declare module 'foo';`
1010

11-
==== /node_modules/foo/index.js (0 errors) ====
11+
==== /node_modules/foo/package.json (0 errors) ====
1212
// This tests that `--noImplicitAny` disables untyped modules.
1313

14+
{ "name": "foo", "version": "1.2.3" }
15+
16+
==== /node_modules/foo/index.js (0 errors) ====
1417
This file is not processed.
1518

tests/baselines/reference/untypedModuleImport_noImplicitAny.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_noImplicitAny.ts] ////
22

3-
//// [index.js]
3+
//// [package.json]
44
// This tests that `--noImplicitAny` disables untyped modules.
55

6+
{ "name": "foo", "version": "1.2.3" }
7+
8+
//// [index.js]
69
This file is not processed.
710

811
//// [a.ts]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/a.ts(1,22): error TS7016: Could not find a declaration file for module './node_modules/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';`
3+
4+
5+
==== /a.ts (1 errors) ====
6+
import * as foo from "./node_modules/foo";
7+
~~~~~~~~~~~~~~~~~~~~
8+
!!! error TS7016: Could not find a declaration file for module './node_modules/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';`
10+
11+
==== /node_modules/foo/package.json (0 errors) ====
12+
{ "name": "foo", "version": "1.2.3" }
13+
14+
==== /node_modules/foo/index.js (0 errors) ====
15+
This file is not processed.
16+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_noImplicitAny_relativePath.ts] ////
2+
3+
//// [package.json]
4+
{ "name": "foo", "version": "1.2.3" }
5+
6+
//// [index.js]
7+
This file is not processed.
8+
9+
//// [a.ts]
10+
import * as foo from "./node_modules/foo";
11+
12+
13+
//// [a.js]
14+
"use strict";
15+
exports.__esModule = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import * as foo from "./node_modules/foo";
3+
>foo : Symbol(foo, Decl(a.ts, 0, 6))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import * as foo from "./node_modules/foo";
3+
>foo : any
4+

tests/cases/conformance/moduleResolution/untypedModuleImport_noImplicitAny.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @noImplicitReferences: true
2-
// @currentDirectory: /
32
// @noImplicitAny: true
43
// This tests that `--noImplicitAny` disables untyped modules.
54

5+
// @filename: /node_modules/foo/package.json
6+
{ "name": "foo", "version": "1.2.3" }
7+
68
// @filename: /node_modules/foo/index.js
79
This file is not processed.
810

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @noImplicitReferences: true
2+
// @noImplicitAny: true
3+
4+
// @filename: /node_modules/foo/package.json
5+
{ "name": "foo", "version": "1.2.3" }
6+
7+
// @filename: /node_modules/foo/index.js
8+
This file is not processed.
9+
10+
// @filename: /a.ts
11+
import * as foo from "./node_modules/foo";

0 commit comments

Comments
 (0)