-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #15540: Throw error when importing @types #15866
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1642,6 +1642,11 @@ namespace ts { | |
return; | ||
} | ||
|
||
if (moduleReference.substr(0, 7) === "@types/") { | ||
const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1; | ||
error(errorNode, diag, moduleReference.substr(7), moduleReference); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a |
||
} | ||
|
||
const ambientModule = tryFindAmbientModule(moduleName, /*withAugmentations*/ true); | ||
if (ambientModule) { | ||
return ambientModule; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2135,6 +2135,10 @@ | |
"category": "Error", | ||
"code": 2710 | ||
}, | ||
"Cannot import type declaration files. Consider importing '{0}' instead of '{1}'.": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put at 6137 near other module resolution diagnostics. |
||
"category": "Error", | ||
"code": 2711 | ||
}, | ||
|
||
"Import declaration '{0}' is using private name '{1}'.": { | ||
"category": "Error", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
tests/cases/compiler/a.ts(1,21): error TS2711: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. | ||
|
||
|
||
==== /node_modules/@types/foo-bar/index.d.ts (0 errors) ==== | ||
export interface Foo { | ||
bar: string; | ||
} | ||
|
||
// This should error | ||
==== tests/cases/compiler/a.ts (1 errors) ==== | ||
import { Foo } from "@types/foo-bar"; | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS2711: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//// [tests/cases/compiler/importDeclTypes.ts] //// | ||
|
||
//// [index.d.ts] | ||
export interface Foo { | ||
bar: string; | ||
} | ||
|
||
// This should error | ||
//// [a.ts] | ||
import { Foo } from "@types/foo-bar"; | ||
|
||
|
||
//// [a.js] | ||
"use strict"; | ||
exports.__esModule = true; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
// @filename: /node_modules/@types/foo-bar/index.d.ts | ||
export interface Foo { | ||
bar: string; | ||
} | ||
|
||
// This should error | ||
// @filename: a.ts | ||
import { Foo } from "@types/foo-bar"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startsWith(moduleReference, "@types/")