Skip to content

Commit 9cea9cf

Browse files
committed
Error if assertion and typeonly import
1 parent 7a5ec34 commit 9cea9cf

11 files changed

+232
-5
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37443,9 +37443,15 @@ namespace ts {
3744337443
}
3744437444
}
3744537445

37446-
function checkGrammarAssertClause(declaration: ImportDeclaration | ExportDeclaration) {
37447-
if (declaration.assertClause && moduleKind !== ModuleKind.ESNext) {
37448-
grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext);
37446+
function checkAssertClause(declaration: ImportDeclaration | ExportDeclaration) {
37447+
if (declaration.assertClause) {
37448+
if (moduleKind !== ModuleKind.ESNext) {
37449+
error(declaration.assertClause, Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext);
37450+
}
37451+
37452+
if (isImportDeclaration(declaration) ? declaration.importClause?.isTypeOnly : declaration.isTypeOnly) {
37453+
error(declaration.assertClause, Diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports);
37454+
}
3744937455
}
3745037456
}
3745137457

@@ -37480,7 +37486,7 @@ namespace ts {
3748037486
}
3748137487
}
3748237488
}
37483-
checkGrammarAssertClause(node);
37489+
checkAssertClause(node);
3748437490
}
3748537491

3748637492
function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
@@ -37575,7 +37581,7 @@ namespace ts {
3757537581
}
3757637582
}
3757737583
}
37578-
checkGrammarAssertClause(node);
37584+
checkAssertClause(node);
3757937585
}
3758037586

3758137587
function checkGrammarExportDeclaration(node: ExportDeclaration): boolean {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,6 +3292,10 @@
32923292
"category": "Error",
32933293
"code": 2803
32943294
},
3295+
"Import assertions cannot be used with type-only imports or exports.": {
3296+
"category": "Error",
3297+
"code": 2804
3298+
},
32953299

32963300
"Import declaration '{0}' is using private name '{1}'.": {
32973301
"category": "Error",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
tests/cases/conformance/importAssertion/1.ts(1,27): error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
2+
tests/cases/conformance/importAssertion/1.ts(1,27): error TS2804: Import assertions cannot be used with type-only imports or exports.
3+
tests/cases/conformance/importAssertion/1.ts(2,30): error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
4+
tests/cases/conformance/importAssertion/1.ts(2,30): error TS2804: Import assertions cannot be used with type-only imports or exports.
5+
tests/cases/conformance/importAssertion/2.ts(1,31): error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
6+
tests/cases/conformance/importAssertion/2.ts(1,31): error TS2804: Import assertions cannot be used with type-only imports or exports.
7+
tests/cases/conformance/importAssertion/2.ts(2,33): error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
8+
tests/cases/conformance/importAssertion/2.ts(2,33): error TS2804: Import assertions cannot be used with type-only imports or exports.
9+
10+
11+
==== tests/cases/conformance/importAssertion/0.ts (0 errors) ====
12+
export interface I { }
13+
14+
==== tests/cases/conformance/importAssertion/1.ts (4 errors) ====
15+
export type {} from './0' assert { type: "json" }
16+
~~~~~~~~~~~~~~~~~~~~~~~
17+
!!! error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
18+
~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
20+
export type { I } from './0' assert { type: "json" }
21+
~~~~~~~~~~~~~~~~~~~~~~~
22+
!!! error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
23+
~~~~~~~~~~~~~~~~~~~~~~~
24+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
25+
26+
==== tests/cases/conformance/importAssertion/2.ts (4 errors) ====
27+
import type { I } from './0' assert { type: "json" }
28+
~~~~~~~~~~~~~~~~~~~~~~~
29+
!!! error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
30+
~~~~~~~~~~~~~~~~~~~~~~~
31+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
32+
import type * as foo from './0' assert { type: "json" }
33+
~~~~~~~~~~~~~~~~~~~~~~~
34+
!!! error TS2803: Import assertions are only supported when the '--module' option is set to 'esnext'.
35+
~~~~~~~~~~~~~~~~~~~~~~~
36+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
37+
38+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/conformance/importAssertion/importAssertion3.ts] ////
2+
3+
//// [0.ts]
4+
export interface I { }
5+
6+
//// [1.ts]
7+
export type {} from './0' assert { type: "json" }
8+
export type { I } from './0' assert { type: "json" }
9+
10+
//// [2.ts]
11+
import type { I } from './0' assert { type: "json" }
12+
import type * as foo from './0' assert { type: "json" }
13+
14+
15+
16+
//// [0.js]
17+
export {};
18+
//// [1.js]
19+
export {};
20+
//// [2.js]
21+
export {};
22+
23+
24+
//// [0.d.ts]
25+
export interface I {
26+
}
27+
//// [1.d.ts]
28+
export type {} from './0';
29+
export type { I } from './0';
30+
//// [2.d.ts]
31+
export {};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/importAssertion/0.ts ===
2+
export interface I { }
3+
>I : Symbol(I, Decl(0.ts, 0, 0))
4+
5+
=== tests/cases/conformance/importAssertion/1.ts ===
6+
export type {} from './0' assert { type: "json" }
7+
export type { I } from './0' assert { type: "json" }
8+
>I : Symbol(I, Decl(1.ts, 1, 13))
9+
10+
=== tests/cases/conformance/importAssertion/2.ts ===
11+
import type { I } from './0' assert { type: "json" }
12+
>I : Symbol(I, Decl(2.ts, 0, 13))
13+
14+
import type * as foo from './0' assert { type: "json" }
15+
>foo : Symbol(foo, Decl(2.ts, 1, 11))
16+
17+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/conformance/importAssertion/0.ts ===
2+
export interface I { }
3+
No type information for this code.
4+
No type information for this code.=== tests/cases/conformance/importAssertion/1.ts ===
5+
export type {} from './0' assert { type: "json" }
6+
>type : any
7+
8+
export type { I } from './0' assert { type: "json" }
9+
>I : import("tests/cases/conformance/importAssertion/0").I
10+
>type : any
11+
12+
=== tests/cases/conformance/importAssertion/2.ts ===
13+
import type { I } from './0' assert { type: "json" }
14+
>I : I
15+
>type : any
16+
17+
import type * as foo from './0' assert { type: "json" }
18+
>foo : typeof foo
19+
>type : any
20+
21+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
tests/cases/conformance/importAssertion/1.ts(1,27): error TS2804: Import assertions cannot be used with type-only imports or exports.
2+
tests/cases/conformance/importAssertion/1.ts(2,30): error TS2804: Import assertions cannot be used with type-only imports or exports.
3+
tests/cases/conformance/importAssertion/2.ts(1,31): error TS2804: Import assertions cannot be used with type-only imports or exports.
4+
tests/cases/conformance/importAssertion/2.ts(2,33): error TS2804: Import assertions cannot be used with type-only imports or exports.
5+
6+
7+
==== tests/cases/conformance/importAssertion/0.ts (0 errors) ====
8+
export interface I { }
9+
10+
==== tests/cases/conformance/importAssertion/1.ts (2 errors) ====
11+
export type {} from './0' assert { type: "json" }
12+
~~~~~~~~~~~~~~~~~~~~~~~
13+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
14+
export type { I } from './0' assert { type: "json" }
15+
~~~~~~~~~~~~~~~~~~~~~~~
16+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
17+
18+
==== tests/cases/conformance/importAssertion/2.ts (2 errors) ====
19+
import type { I } from './0' assert { type: "json" }
20+
~~~~~~~~~~~~~~~~~~~~~~~
21+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
22+
import type * as foo from './0' assert { type: "json" }
23+
~~~~~~~~~~~~~~~~~~~~~~~
24+
!!! error TS2804: Import assertions cannot be used with type-only imports or exports.
25+
26+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/conformance/importAssertion/importAssertion3.ts] ////
2+
3+
//// [0.ts]
4+
export interface I { }
5+
6+
//// [1.ts]
7+
export type {} from './0' assert { type: "json" }
8+
export type { I } from './0' assert { type: "json" }
9+
10+
//// [2.ts]
11+
import type { I } from './0' assert { type: "json" }
12+
import type * as foo from './0' assert { type: "json" }
13+
14+
15+
16+
//// [0.js]
17+
export {};
18+
//// [1.js]
19+
export {};
20+
//// [2.js]
21+
export {};
22+
23+
24+
//// [0.d.ts]
25+
export interface I {
26+
}
27+
//// [1.d.ts]
28+
export type {} from './0';
29+
export type { I } from './0';
30+
//// [2.d.ts]
31+
export {};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/importAssertion/0.ts ===
2+
export interface I { }
3+
>I : Symbol(I, Decl(0.ts, 0, 0))
4+
5+
=== tests/cases/conformance/importAssertion/1.ts ===
6+
export type {} from './0' assert { type: "json" }
7+
export type { I } from './0' assert { type: "json" }
8+
>I : Symbol(I, Decl(1.ts, 1, 13))
9+
10+
=== tests/cases/conformance/importAssertion/2.ts ===
11+
import type { I } from './0' assert { type: "json" }
12+
>I : Symbol(I, Decl(2.ts, 0, 13))
13+
14+
import type * as foo from './0' assert { type: "json" }
15+
>foo : Symbol(foo, Decl(2.ts, 1, 11))
16+
17+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/conformance/importAssertion/0.ts ===
2+
export interface I { }
3+
No type information for this code.
4+
No type information for this code.=== tests/cases/conformance/importAssertion/1.ts ===
5+
export type {} from './0' assert { type: "json" }
6+
>type : any
7+
8+
export type { I } from './0' assert { type: "json" }
9+
>I : import("tests/cases/conformance/importAssertion/0").I
10+
>type : any
11+
12+
=== tests/cases/conformance/importAssertion/2.ts ===
13+
import type { I } from './0' assert { type: "json" }
14+
>I : I
15+
>type : any
16+
17+
import type * as foo from './0' assert { type: "json" }
18+
>foo : typeof foo
19+
>type : any
20+
21+

0 commit comments

Comments
 (0)