Skip to content

Commit bf7bfa1

Browse files
authored
fix(48445): show errors on type-only import/export specifiers in JavaScript files (microsoft#48449)
1 parent d962091 commit bf7bfa1

9 files changed

+73
-0
lines changed

Diff for: src/compiler/program.ts

+7
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,13 @@ namespace ts {
22732273
return "skip";
22742274
}
22752275
break;
2276+
case SyntaxKind.ImportSpecifier:
2277+
case SyntaxKind.ExportSpecifier:
2278+
if ((node as ImportOrExportSpecifier).isTypeOnly) {
2279+
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, isImportSpecifier(node) ? "import...type" : "export...type"));
2280+
return "skip";
2281+
}
2282+
break;
22762283
case SyntaxKind.ImportEqualsDeclaration:
22772284
diagnostics.push(createDiagnosticForNode(node, Diagnostics.import_can_only_be_used_in_TypeScript_files));
22782285
return "skip";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/externalModules/typeOnly/a.js(2,10): error TS8006: 'export...type' declarations can only be used in TypeScript files.
2+
3+
4+
==== tests/cases/conformance/externalModules/typeOnly/a.js (1 errors) ====
5+
const foo = 0;
6+
export { type foo };
7+
~~~~~~~~
8+
!!! error TS8006: 'export...type' declarations can only be used in TypeScript files.
9+
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/a.js ===
2+
const foo = 0;
3+
>foo : Symbol(foo, Decl(a.js, 0, 5))
4+
5+
export { type foo };
6+
>foo : Symbol(foo, Decl(a.js, 1, 8))
7+

Diff for: tests/baselines/reference/exportSpecifiers_js.types

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/a.js ===
2+
const foo = 0;
3+
>foo : 0
4+
>0 : 0
5+
6+
export { type foo };
7+
>foo : 0
8+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/externalModules/typeOnly/a.js(1,10): error TS8006: 'import...type' declarations can only be used in TypeScript files.
2+
3+
4+
==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ====
5+
export interface A {}
6+
7+
==== tests/cases/conformance/externalModules/typeOnly/a.js (1 errors) ====
8+
import { type A } from "./a";
9+
~~~~~~
10+
!!! error TS8006: 'import...type' declarations can only be used in TypeScript files.
11+
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
2+
export interface A {}
3+
>A : Symbol(A, Decl(a.ts, 0, 0))
4+
5+
=== tests/cases/conformance/externalModules/typeOnly/a.js ===
6+
import { type A } from "./a";
7+
>A : Symbol(A, Decl(a.js, 0, 8))
8+

Diff for: tests/baselines/reference/importSpecifiers_js.types

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
2+
export interface A {}
3+
No type information for this code.
4+
No type information for this code.=== tests/cases/conformance/externalModules/typeOnly/a.js ===
5+
import { type A } from "./a";
6+
>A : any
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
5+
// @Filename: ./a.js
6+
const foo = 0;
7+
export { type foo };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
5+
// @Filename: ./a.ts
6+
export interface A {}
7+
8+
// @Filename: ./a.js
9+
import { type A } from "./a";

0 commit comments

Comments
 (0)