Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e4e4b17

Browse filesBrowse files
author
Andy
authored
Improve error message for untyped import of scoped package (microsoft#22189)
1 parent b31aa4e commit e4e4b17
Copy full SHA for e4e4b17

7 files changed

+53-2Lines changed: 53 additions & 2 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,7 @@ namespace ts {
20922092
else if (noImplicitAny && moduleNotFoundError) {
20932093
let errorInfo = resolvedModule.packageId && chainDiagnosticMessages(/*details*/ undefined,
20942094
Diagnostics.Try_npm_install_types_Slash_0_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
2095-
resolvedModule.packageId.name);
2095+
getMangledNameForScopedPackage(resolvedModule.packageId.name));
20962096
errorInfo = chainDiagnosticMessages(errorInfo,
20972097
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
20982098
moduleReference,
Collapse file

‎src/compiler/moduleNameResolver.ts‎

Copy file name to clipboardExpand all lines: src/compiler/moduleNameResolver.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,8 @@ namespace ts {
11571157
return `@types/${getMangledNameForScopedPackage(packageName)}`;
11581158
}
11591159

1160-
function getMangledNameForScopedPackage(packageName: string): string {
1160+
/* @internal */
1161+
export function getMangledNameForScopedPackage(packageName: string): string {
11611162
if (startsWith(packageName, "@")) {
11621163
const replaceSlash = packageName.replace(ts.directorySeparator, mangledScopedPackageSeparator);
11631164
if (replaceSlash !== packageName) {
Collapse file
+16Lines 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 '@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';`
3+
4+
5+
==== /a.ts (1 errors) ====
6+
import * as foo from "@foo/bar";
7+
~~~~~~~~~~
8+
!!! 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';`
10+
11+
==== /node_modules/@foo/bar/package.json (0 errors) ====
12+
{ "name": "@foo/bar", "version": "1.2.3" }
13+
14+
==== /node_modules/@foo/bar/index.js (0 errors) ====
15+
This file is not processed.
16+
Collapse file
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_noImplicitAny_scoped.ts] ////
2+
3+
//// [package.json]
4+
{ "name": "@foo/bar", "version": "1.2.3" }
5+
6+
//// [index.js]
7+
This file is not processed.
8+
9+
//// [a.ts]
10+
import * as foo from "@foo/bar";
11+
12+
13+
//// [a.js]
14+
"use strict";
15+
exports.__esModule = true;
Collapse file
+4Lines 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 "@foo/bar";
3+
>foo : Symbol(foo, Decl(a.ts, 0, 6))
4+
Collapse file
+4Lines 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 "@foo/bar";
3+
>foo : any
4+
Collapse file
+11Lines 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/bar/package.json
5+
{ "name": "@foo/bar", "version": "1.2.3" }
6+
7+
// @filename: /node_modules/@foo/bar/index.js
8+
This file is not processed.
9+
10+
// @filename: /a.ts
11+
import * as foo from "@foo/bar";

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.