Skip to content

Commit d054621

Browse files
committed
"Could not find a declaration file for module" error needs to use the
unmangled package name where appropriate. Add a test case for an untyped sub-module of a scoped package with typings. The other diagnostic message is covered by existing tests; I guess no one looked at the baselines closely enough. Fixes microsoft#23999.
1 parent a5a26ec commit d054621

8 files changed

+107
-7
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,8 +2244,9 @@ namespace ts {
22442244
? chainDiagnosticMessages(
22452245
/*details*/ undefined,
22462246
typesPackageExists(packageId.name)
2247-
? Diagnostics.If_the_0_package_actually_exposes_this_module_consider_sending_a_pull_request_to_amend_https_Colon_Slash_Slashgithub_com_SlashDefinitelyTyped_SlashDefinitelyTyped_Slashtree_Slashmaster_Slashtypes_Slash_0
2248-
: Diagnostics.Try_npm_install_types_Slash_0_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
2247+
? Diagnostics.If_the_0_package_actually_exposes_this_module_consider_sending_a_pull_request_to_amend_https_Colon_Slash_Slashgithub_com_SlashDefinitelyTyped_SlashDefinitelyTyped_Slashtree_Slashmaster_Slashtypes_Slash_1
2248+
: Diagnostics.Try_npm_install_types_Slash_1_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
2249+
packageId.name,
22492250
getMangledNameForScopedPackage(packageId.name))
22502251
: undefined;
22512252
errorOrSuggestion(isError, errorNode, chainDiagnosticMessages(

src/compiler/diagnosticMessages.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,7 +3901,7 @@
39013901
"category": "Error",
39023902
"code": 7034
39033903
},
3904-
"Try `npm install @types/{0}` if it exists or add a new declaration (.d.ts) file containing `declare module '{0}';`": {
3904+
"Try `npm install @types/{1}` if it exists or add a new declaration (.d.ts) file containing `declare module '{0}';`": {
39053905
"category": "Error",
39063906
"code": 7035
39073907
},
@@ -3921,7 +3921,7 @@
39213921
"category": "Error",
39223922
"code": 7039
39233923
},
3924-
"If the '{0}' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/{0}`": {
3924+
"If the '{0}' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/{1}`": {
39253925
"category": "Error",
39263926
"code": 7040
39273927
},

tests/baselines/reference/untypedModuleImport_noImplicitAny_scoped.errors.txt

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

44

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

1111
==== /node_modules/@foo/bar/package.json (0 errors) ====
1212
{ "name": "@foo/bar", "version": "1.2.3" }

tests/baselines/reference/untypedModuleImport_noImplicitAny_typesForPackageExist.errors.txt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
If the 'foo' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/foo`
33
/a.ts(3,25): error TS7016: Could not find a declaration file for module 'bar/sub'. '/node_modules/bar/sub.js' implicitly has an 'any' type.
44
Try `npm install @types/bar` if it exists or add a new declaration (.d.ts) file containing `declare module 'bar';`
5+
/a.ts(5,30): error TS7016: Could not find a declaration file for module '@scope/foo/sub'. '/node_modules/@scope/foo/sub.js' implicitly has an 'any' type.
6+
If the '@scope/foo' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/scope__foo`
7+
/a.ts(6,30): error TS7016: Could not find a declaration file for module '@scope/bar/sub'. '/node_modules/@scope/bar/sub.js' implicitly has an 'any' type.
8+
Try `npm install @types/scope__bar` if it exists or add a new declaration (.d.ts) file containing `declare module '@scope/bar';`
59

610

7-
==== /a.ts (2 errors) ====
11+
==== /a.ts (4 errors) ====
812
import * as foo from "foo";
913
import * as fooSub from "foo/sub";
1014
~~~~~~~~~
@@ -14,6 +18,15 @@
1418
~~~~~~~~~
1519
!!! error TS7016: Could not find a declaration file for module 'bar/sub'. '/node_modules/bar/sub.js' implicitly has an 'any' type.
1620
!!! error TS7016: Try `npm install @types/bar` if it exists or add a new declaration (.d.ts) file containing `declare module 'bar';`
21+
import * as scopeFoo from "@scope/foo";
22+
import * as scopeFooSub from "@scope/foo/sub";
23+
~~~~~~~~~~~~~~~~
24+
!!! error TS7016: Could not find a declaration file for module '@scope/foo/sub'. '/node_modules/@scope/foo/sub.js' implicitly has an 'any' type.
25+
!!! error TS7016: If the '@scope/foo' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/scope__foo`
26+
import * as scopeBarSub from "@scope/bar/sub";
27+
~~~~~~~~~~~~~~~~
28+
!!! error TS7016: Could not find a declaration file for module '@scope/bar/sub'. '/node_modules/@scope/bar/sub.js' implicitly has an 'any' type.
29+
!!! error TS7016: Try `npm install @types/scope__bar` if it exists or add a new declaration (.d.ts) file containing `declare module '@scope/bar';`
1730

1831
==== /node_modules/@types/foo/index.d.ts (0 errors) ====
1932
export const foo: number;
@@ -32,4 +45,22 @@
3245

3346
==== /node_modules/bar/package.json (0 errors) ====
3447
{ "name": "bar", "version": "1.2.3" }
48+
49+
==== /node_modules/@types/scope__foo/index.d.ts (0 errors) ====
50+
export const foo: number;
51+
52+
==== /node_modules/@types/scope__foo/package.json (0 errors) ====
53+
{ "name": "@types/scope__foo", "version": "1.2.3" }
54+
55+
==== /node_modules/@scope/foo/sub.js (0 errors) ====
56+
const x = 0;
57+
58+
==== /node_modules/@scope/foo/package.json (0 errors) ====
59+
{ "name": "@scope/foo", "version": "1.2.3" }
60+
61+
==== /node_modules/@scope/bar/sub.js (0 errors) ====
62+
const x = 0;
63+
64+
==== /node_modules/@scope/bar/package.json (0 errors) ====
65+
{ "name": "@scope/bar", "version": "1.2.3" }
3566

tests/baselines/reference/untypedModuleImport_noImplicitAny_typesForPackageExist.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,31 @@ const x = 0;
1818
//// [package.json]
1919
{ "name": "bar", "version": "1.2.3" }
2020

21+
//// [index.d.ts]
22+
export const foo: number;
23+
24+
//// [package.json]
25+
{ "name": "@types/scope__foo", "version": "1.2.3" }
26+
27+
//// [sub.js]
28+
const x = 0;
29+
30+
//// [package.json]
31+
{ "name": "@scope/foo", "version": "1.2.3" }
32+
33+
//// [sub.js]
34+
const x = 0;
35+
36+
//// [package.json]
37+
{ "name": "@scope/bar", "version": "1.2.3" }
38+
2139
//// [a.ts]
2240
import * as foo from "foo";
2341
import * as fooSub from "foo/sub";
2442
import * as barSub from "bar/sub";
43+
import * as scopeFoo from "@scope/foo";
44+
import * as scopeFooSub from "@scope/foo/sub";
45+
import * as scopeBarSub from "@scope/bar/sub";
2546

2647

2748
//// [a.js]

tests/baselines/reference/untypedModuleImport_noImplicitAny_typesForPackageExist.symbols

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ import * as fooSub from "foo/sub";
88
import * as barSub from "bar/sub";
99
>barSub : Symbol(barSub, Decl(a.ts, 2, 6))
1010

11+
import * as scopeFoo from "@scope/foo";
12+
>scopeFoo : Symbol(scopeFoo, Decl(a.ts, 3, 6))
13+
14+
import * as scopeFooSub from "@scope/foo/sub";
15+
>scopeFooSub : Symbol(scopeFooSub, Decl(a.ts, 4, 6))
16+
17+
import * as scopeBarSub from "@scope/bar/sub";
18+
>scopeBarSub : Symbol(scopeBarSub, Decl(a.ts, 5, 6))
19+
1120
=== /node_modules/@types/foo/index.d.ts ===
1221
export const foo: number;
1322
>foo : Symbol(foo, Decl(index.d.ts, 0, 12))
1423

24+
=== /node_modules/@types/scope__foo/index.d.ts ===
25+
export const foo: number;
26+
>foo : Symbol(foo, Decl(index.d.ts, 0, 12))
27+

tests/baselines/reference/untypedModuleImport_noImplicitAny_typesForPackageExist.types

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ import * as fooSub from "foo/sub";
88
import * as barSub from "bar/sub";
99
>barSub : any
1010

11+
import * as scopeFoo from "@scope/foo";
12+
>scopeFoo : typeof scopeFoo
13+
14+
import * as scopeFooSub from "@scope/foo/sub";
15+
>scopeFooSub : any
16+
17+
import * as scopeBarSub from "@scope/bar/sub";
18+
>scopeBarSub : any
19+
1120
=== /node_modules/@types/foo/index.d.ts ===
1221
export const foo: number;
1322
>foo : number
1423

24+
=== /node_modules/@types/scope__foo/index.d.ts ===
25+
export const foo: number;
26+
>foo : number
27+

tests/cases/conformance/moduleResolution/untypedModuleImport_noImplicitAny_typesForPackageExist.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@ const x = 0;
1919
// @Filename: /node_modules/bar/package.json
2020
{ "name": "bar", "version": "1.2.3" }
2121

22+
// @Filename: /node_modules/@types/scope__foo/index.d.ts
23+
export const foo: number;
24+
25+
// @Filename: /node_modules/@types/scope__foo/package.json
26+
{ "name": "@types/scope__foo", "version": "1.2.3" }
27+
28+
// @Filename: /node_modules/@scope/foo/sub.js
29+
const x = 0;
30+
31+
// @Filename: /node_modules/@scope/foo/package.json
32+
{ "name": "@scope/foo", "version": "1.2.3" }
33+
34+
// @Filename: /node_modules/@scope/bar/sub.js
35+
const x = 0;
36+
37+
// @Filename: /node_modules/@scope/bar/package.json
38+
{ "name": "@scope/bar", "version": "1.2.3" }
39+
2240
// @Filename: /a.ts
2341
import * as foo from "foo";
2442
import * as fooSub from "foo/sub";
2543
import * as barSub from "bar/sub";
44+
import * as scopeFoo from "@scope/foo";
45+
import * as scopeFooSub from "@scope/foo/sub";
46+
import * as scopeBarSub from "@scope/bar/sub";

0 commit comments

Comments
 (0)