Skip to content

Commit 2ffe7d0

Browse files
committed
Disallow import assertions in nodenext
1 parent 4f80655 commit 2ffe7d0

7 files changed

+55
-26
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48076,6 +48076,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4807648076
);
4807748077
}
4807848078

48079+
if (moduleKind === ModuleKind.NodeNext && !isImportAttributes) {
48080+
return grammarErrorOnFirstToken(node, Diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_asserts);
48081+
}
48082+
4807948083
if (declaration.moduleSpecifier && getEmitSyntaxForModuleSpecifierExpression(declaration.moduleSpecifier) === ModuleKind.CommonJS) {
4808048084
return grammarErrorOnNode(
4808148085
node,

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,10 @@
39803980
"category": "Error",
39813981
"code": 2879
39823982
},
3983+
"Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.": {
3984+
"category": "Error",
3985+
"code": 2880
3986+
},
39833987

39843988
"Import declaration '{0}' is using private name '{1}'.": {
39853989
"category": "Error",

tests/baselines/reference/importAssertionNonstring.errors.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1+
mod.mts(1,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
12
mod.mts(1,37): error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'.
23
Property 'field' is incompatible with index signature.
34
Type 'number' is not assignable to type 'string'.
45
mod.mts(1,52): error TS2837: Import assertion values must be string literal expressions.
6+
mod.mts(3,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
57
mod.mts(3,52): error TS2837: Import assertion values must be string literal expressions.
8+
mod.mts(5,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
69
mod.mts(5,37): error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'.
710
Property 'field' is incompatible with index signature.
811
Type 'RegExp' is not assignable to type 'string'.
912
mod.mts(5,52): error TS2837: Import assertion values must be string literal expressions.
13+
mod.mts(7,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
1014
mod.mts(7,37): error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'.
1115
Property 'field' is incompatible with index signature.
1216
Type 'string[]' is not assignable to type 'string'.
1317
mod.mts(7,52): error TS2837: Import assertion values must be string literal expressions.
18+
mod.mts(9,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
1419
mod.mts(9,37): error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'.
1520
Property 'field' is incompatible with index signature.
1621
Type '{ a: number; }' is not assignable to type 'string'.
1722
mod.mts(9,52): error TS2837: Import assertion values must be string literal expressions.
23+
mod.mts(11,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
1824
mod.mts(11,66): error TS2837: Import assertion values must be string literal expressions.
1925

2026

21-
==== mod.mts (10 errors) ====
27+
==== mod.mts (16 errors) ====
2228
import * as thing1 from "./mod.mjs" assert {field: 0};
29+
~~~~~~
30+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
2331
~~~~~~~~~~~~~~~~~
2432
!!! error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'.
2533
!!! error TS2322: Property 'field' is incompatible with index signature.
@@ -28,10 +36,14 @@ mod.mts(11,66): error TS2837: Import assertion values must be string literal exp
2836
!!! error TS2837: Import assertion values must be string literal expressions.
2937

3038
import * as thing2 from "./mod.mjs" assert {field: `a`};
39+
~~~~~~
40+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
3141
~~~
3242
!!! error TS2837: Import assertion values must be string literal expressions.
3343

3444
import * as thing3 from "./mod.mjs" assert {field: /a/g};
45+
~~~~~~
46+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
3547
~~~~~~~~~~~~~~~~~~~~
3648
!!! error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'.
3749
!!! error TS2322: Property 'field' is incompatible with index signature.
@@ -40,6 +52,8 @@ mod.mts(11,66): error TS2837: Import assertion values must be string literal exp
4052
!!! error TS2837: Import assertion values must be string literal expressions.
4153

4254
import * as thing4 from "./mod.mjs" assert {field: ["a"]};
55+
~~~~~~
56+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
4357
~~~~~~~~~~~~~~~~~~~~~
4458
!!! error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'.
4559
!!! error TS2322: Property 'field' is incompatible with index signature.
@@ -48,6 +62,8 @@ mod.mts(11,66): error TS2837: Import assertion values must be string literal exp
4862
!!! error TS2837: Import assertion values must be string literal expressions.
4963

5064
import * as thing5 from "./mod.mjs" assert {field: { a: 0 }};
65+
~~~~~~
66+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
5167
~~~~~~~~~~~~~~~~~~~~~~~~
5268
!!! error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'.
5369
!!! error TS2322: Property 'field' is incompatible with index signature.
@@ -56,5 +72,7 @@ mod.mts(11,66): error TS2837: Import assertion values must be string literal exp
5672
!!! error TS2837: Import assertion values must be string literal expressions.
5773

5874
import * as thing6 from "./mod.mjs" assert {type: "json", field: 0..toString()}
75+
~~~~~~
76+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
5977
~~~~~~~~~~~~~
6078
!!! error TS2837: Import assertion values must be string literal expressions.

tests/baselines/reference/nodeModulesImportAssertions(module=nodenext).errors.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
otherc.cts(1,35): error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
1+
index.ts(1,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
2+
otherc.cts(1,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
23

34

4-
==== index.ts (0 errors) ====
5+
==== index.ts (1 errors) ====
56
import json from "./package.json" assert { type: "json" };
7+
~~~~~~
8+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
69
==== otherc.cts (1 errors) ====
710
import json from "./package.json" assert { type: "json" }; // should error, cjs mode imports don't support assertions
8-
~~~~~~~~~~~~~~~~~~~~~~~
9-
!!! error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
11+
~~~~~~
12+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
1013
const json2 = import("./package.json", { assert: { type: "json" } }); // should be fine
1114
==== package.json (0 errors) ====
1215
{

tests/baselines/reference/nodeModulesImportModeDeclarationEmit1(module=nodenext).errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/index.ts(6,50): error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
1+
/index.ts(6,50): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
22
/index.ts(7,14): error TS2305: Module '"pkg"' has no exported member 'ImportInterface'.
3-
/index.ts(7,49): error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
3+
/index.ts(7,49): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
44

55

66
==== /index.ts (3 errors) ====
@@ -10,13 +10,13 @@
1010
export interface LocalInterface extends RequireInterface, ImportInterface {}
1111

1212
import {type RequireInterface as Req} from "pkg" assert { "resolution-mode": "require" };
13-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14-
!!! error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
13+
~~~~~~
14+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
1515
import {type ImportInterface as Imp} from "pkg" assert { "resolution-mode": "import" };
1616
~~~~~~~~~~~~~~~
1717
!!! error TS2305: Module '"pkg"' has no exported member 'ImportInterface'.
18-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19-
!!! error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
18+
~~~~~~
19+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
2020
export interface Loc extends Req, Imp {}
2121

2222
export type { RequireInterface } from "pkg" assert { "resolution-mode": "require" };

tests/baselines/reference/nodeModulesImportModeDeclarationEmit2(module=nodenext).errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/index.ts(6,14): error TS2305: Module '"pkg"' has no exported member 'RequireInterface'.
2-
/index.ts(6,50): error TS1454: `resolution-mode` can only be set for type-only imports.
3-
/index.ts(7,49): error TS1454: `resolution-mode` can only be set for type-only imports.
2+
/index.ts(6,50): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
3+
/index.ts(7,49): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
44

55

66
==== /index.ts (3 errors) ====
@@ -12,11 +12,11 @@
1212
import {type RequireInterface as Req} from "pkg" assert { "resolution-mode": "require" };
1313
~~~~~~~~~~~~~~~~
1414
!!! error TS2305: Module '"pkg"' has no exported member 'RequireInterface'.
15-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16-
!!! error TS1454: `resolution-mode` can only be set for type-only imports.
15+
~~~~~~
16+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
1717
import {type ImportInterface as Imp} from "pkg" assert { "resolution-mode": "import" };
18-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19-
!!! error TS1454: `resolution-mode` can only be set for type-only imports.
18+
~~~~~~
19+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
2020
export interface Loc extends Req, Imp {}
2121

2222
export type { RequireInterface } from "pkg" assert { "resolution-mode": "require" };

tests/baselines/reference/nodeModulesImportModeDeclarationEmitErrors1(module=nodenext).errors.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
/index.ts(2,45): error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
1+
/index.ts(2,45): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
22
/index.ts(2,73): error TS1453: `resolution-mode` should be either `require` or `import`.
33
/index.ts(4,10): error TS2305: Module '"pkg"' has no exported member 'ImportInterface'.
4-
/index.ts(4,39): error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
5-
/index.ts(6,76): error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
4+
/index.ts(4,39): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
5+
/index.ts(6,76): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
66

77

88
==== /index.ts (5 errors) ====
99
// incorrect mode
1010
import type { RequireInterface } from "pkg" assert { "resolution-mode": "foobar" };
11-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12-
!!! error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
11+
~~~~~~
12+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
1313
~~~~~~~~
1414
!!! error TS1453: `resolution-mode` should be either `require` or `import`.
1515
// not type-only
1616
import { ImportInterface } from "pkg" assert { "resolution-mode": "import" };
1717
~~~~~~~~~~~~~~~
1818
!!! error TS2305: Module '"pkg"' has no exported member 'ImportInterface'.
19-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20-
!!! error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
19+
~~~~~~
20+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
2121
// not exclusively type-only
2222
import {type RequireInterface as Req, RequireInterface as Req2} from "pkg" assert { "resolution-mode": "require" };
23-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24-
!!! error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
23+
~~~~~~
24+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.
2525

2626
export interface LocalInterface extends RequireInterface, ImportInterface {}
2727

0 commit comments

Comments
 (0)