Skip to content
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

Merged
merged 2 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,12 @@ namespace ts {
return;
}

if (startsWith(moduleReference, "@types/")) {
const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1;
const withoutAtTypePrefix = removePrefix(moduleReference, "@types/");
error(errorNode, diag, withoutAtTypePrefix, moduleReference);
}

const ambientModule = tryFindAmbientModule(moduleName, /*withAugmentations*/ true);
if (ambientModule) {
return ambientModule;
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,11 @@ namespace ts {
return str.lastIndexOf(prefix, 0) === 0;
}

/* @internal */
export function removePrefix(str: string, prefix: string): string {
return startsWith(str, prefix) ? str.substr(prefix.length) : str;
}

/* @internal */
export function endsWith(str: string, suffix: string): boolean {
const expectedPos = str.length - suffix.length;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3037,6 +3037,10 @@
"category": "Message",
"code": 6136
},
"Cannot import type declaration files. Consider importing '{0}' instead of '{1}'.": {
"category": "Error",
"code": 6137
},
"Property '{0}' is declared but never used.": {
"category": "Error",
"code": 6138
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/importDeclTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/compiler/a.ts(1,21): error TS6137: 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 TS6137: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'.

15 changes: 15 additions & 0 deletions tests/baselines/reference/importDeclTypes.js
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;
9 changes: 9 additions & 0 deletions tests/cases/compiler/importDeclTypes.ts
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";