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 4cd20b1

Browse filesBrowse files
nowymhegazy
authored andcommitted
Fix microsoft#15540: Throw error when importing @types (microsoft#15866)
* Fix microsoft#15540: Throw error when importing @types Fix issue: microsoft#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
Copy full SHA for 4cd20b1

6 files changed

+53Lines changed: 53 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
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
+6Lines changed: 6 additions & 0 deletions
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;
Collapse file

‎src/compiler/core.ts‎

Copy file name to clipboardExpand all lines: src/compiler/core.ts
+5Lines changed: 5 additions & 0 deletions
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;
Collapse file

‎src/compiler/diagnosticMessages.json‎

Copy file name to clipboardExpand all lines: src/compiler/diagnosticMessages.json
+4Lines changed: 4 additions & 0 deletions
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
Collapse file
+14Lines changed: 14 additions & 0 deletions
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+
Collapse file
+15Lines changed: 15 additions & 0 deletions
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;
Collapse file
+9Lines changed: 9 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.