Skip to content

Commit 4cd20b1

Browse files
nowymhegazy
authored andcommitted
Fix #15540: Throw error when importing @types (#15866)
* Fix #15540: Throw error when importing @types Fix issue: #15540 - Modify checker; external imports to account for imported modules containing '@types/'. - Add diagnostic message. - Add test case * FIX-15540: Review changes - Replace `substr` with `startsWith` - move diagnostics message to more relevant place - Add `removePrefix` helper function
1 parent 38ece3b commit 4cd20b1

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

src/compiler/checker.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,12 @@ namespace ts {
16421642
return;
16431643
}
16441644

1645+
if (startsWith(moduleReference, "@types/")) {
1646+
const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1;
1647+
const withoutAtTypePrefix = removePrefix(moduleReference, "@types/");
1648+
error(errorNode, diag, withoutAtTypePrefix, moduleReference);
1649+
}
1650+
16451651
const ambientModule = tryFindAmbientModule(moduleName, /*withAugmentations*/ true);
16461652
if (ambientModule) {
16471653
return ambientModule;

src/compiler/core.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,11 @@ namespace ts {
17641764
return str.lastIndexOf(prefix, 0) === 0;
17651765
}
17661766

1767+
/* @internal */
1768+
export function removePrefix(str: string, prefix: string): string {
1769+
return startsWith(str, prefix) ? str.substr(prefix.length) : str;
1770+
}
1771+
17671772
/* @internal */
17681773
export function endsWith(str: string, suffix: string): boolean {
17691774
const expectedPos = str.length - suffix.length;

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3061,6 +3061,10 @@
30613061
"category": "Message",
30623062
"code": 6136
30633063
},
3064+
"Cannot import type declaration files. Consider importing '{0}' instead of '{1}'.": {
3065+
"category": "Error",
3066+
"code": 6137
3067+
},
30643068
"Property '{0}' is declared but never used.": {
30653069
"category": "Error",
30663070
"code": 6138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/a.ts(1,21): error TS6137: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'.
2+
3+
4+
==== /node_modules/@types/foo-bar/index.d.ts (0 errors) ====
5+
export interface Foo {
6+
bar: string;
7+
}
8+
9+
// This should error
10+
==== tests/cases/compiler/a.ts (1 errors) ====
11+
import { Foo } from "@types/foo-bar";
12+
~~~~~~~~~~~~~~~~
13+
!!! error TS6137: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'.
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/importDeclTypes.ts] ////
2+
3+
//// [index.d.ts]
4+
export interface Foo {
5+
bar: string;
6+
}
7+
8+
// This should error
9+
//// [a.ts]
10+
import { Foo } from "@types/foo-bar";
11+
12+
13+
//// [a.js]
14+
"use strict";
15+
exports.__esModule = true;
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
// @filename: /node_modules/@types/foo-bar/index.d.ts
3+
export interface Foo {
4+
bar: string;
5+
}
6+
7+
// This should error
8+
// @filename: a.ts
9+
import { Foo } from "@types/foo-bar";

0 commit comments

Comments
 (0)