mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
* 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
This commit is contained in:
committed by
Mohamed Hegazy
parent
38ece3b703
commit
4cd20b1335
@@ -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;
|
||||
|
||||
@@ -1764,6 +1764,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;
|
||||
|
||||
@@ -3061,6 +3061,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
|
||||
|
||||
@@ -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'.
|
||||
|
||||
@@ -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;
|
||||
@@ -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";
|
||||
Reference in New Issue
Block a user