Skip to content

Commit ad23f0b

Browse files
committed
fix(48445): show errors on type-only import/export specifiers in JavaScript files
1 parent 1ed1a23 commit ad23f0b

9 files changed

+73
-0
lines changed

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, getSourceTextOfNodeFromSourceFile(sourceFile, node)));
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: 'type foo' 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: 'type foo' declarations can only be used in TypeScript files.
9+
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+
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/externalModules/typeOnly/a.js(1,10): error TS8006: 'type A' 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: 'type A' declarations can only be used in TypeScript files.
11+
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+
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)