Skip to content

Commit 21b02a1

Browse files
🤖 Pick PR #60749 (Do not require import attribute on ...) into release-5.7 (#60750)
Co-authored-by: Andrew Branch <[email protected]>
1 parent b82fd16 commit 21b02a1

8 files changed

+61
-17
lines changed

‎src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47660,7 +47660,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4766047660
}
4766147661
}
4766247662

47663-
if (moduleKind === ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
47663+
if (!importClause.isTypeOnly && moduleKind === ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
4766447664
// Import attributes/assertions are not allowed in --module node16, so don't suggest adding one
4766547665
error(node.moduleSpecifier, Diagnostics.Importing_a_JSON_file_into_an_ECMAScript_module_requires_a_type_Colon_json_import_attribute_when_module_is_set_to_0, ModuleKind[moduleKind]);
4766647666
}

‎tests/baselines/reference/nodeModulesJson(module=node16).errors.txt

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
/loosey.cts(6,9): error TS2339: Property 'default' does not exist on type '{ version: number; }'.
33
/main.mts(5,36): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
44
/main.mts(6,52): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
5-
/main.mts(8,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.
6-
/main.mts(8,41): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
7-
/main.mts(9,42): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
8-
/main.mts(10,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
5+
/main.mts(9,47): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
6+
/main.mts(10,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.
7+
/main.mts(10,41): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
8+
/main.mts(11,42): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
9+
/main.mts(12,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
910

1011

1112
==== /node_modules/not.json/package.json (0 errors) ====
@@ -42,7 +43,7 @@
4243
"version": 1
4344
}
4445

45-
==== /main.mts (6 errors) ====
46+
==== /main.mts (7 errors) ====
4647
import { oops } from "not.json"; // Ok
4748
import moreOops from "actually-json"; // Error in nodenext
4849
import typed from "actually-json/typed"; // Error in nodenext
@@ -54,6 +55,10 @@
5455
~~~~~~~~~~~~~~~~~~~~~
5556
!!! error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
5657
import config2 from "./config.json"; // Error in nodenext, no attribute
58+
import type config2Type from "./config.json"; // Ok, type-only
59+
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
60+
~~~~~~~~~~~~~~~~~~~~~
61+
!!! error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
5762
import { version } from "./config.json" with { type: "json" }; // Error, named import
5863
~~~~~~~
5964
!!! error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.

‎tests/baselines/reference/nodeModulesJson(module=node16).symbols

+10-4
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,24 @@ import { default as config1 } from "./config.json" with { type: "json" }; // Ok
4242
import config2 from "./config.json"; // Error in nodenext, no attribute
4343
>config2 : Symbol(config2, Decl(main.mts, 6, 6))
4444

45+
import type config2Type from "./config.json"; // Ok, type-only
46+
>config2Type : Symbol(config2Type, Decl(main.mts, 7, 6))
47+
48+
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
49+
>config2Type2 : Symbol(config2Type2, Decl(main.mts, 8, 6))
50+
4551
import { version } from "./config.json" with { type: "json" }; // Error, named import
46-
>version : Symbol(version, Decl(main.mts, 7, 8))
52+
>version : Symbol(version, Decl(main.mts, 9, 8))
4753

4854
import * as config3 from "./config.json" with { type: "json" };
49-
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
55+
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
5056

5157
config3.version; // Error
52-
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
58+
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
5359

5460
config3.default; // Ok
5561
>config3.default : Symbol("/config")
56-
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
62+
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
5763
>default : Symbol("/config")
5864

5965
=== /loosey.cts ===

‎tests/baselines/reference/nodeModulesJson(module=node16).types

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ import config2 from "./config.json"; // Error in nodenext, no attribute
6464
>config2 : { version: number; }
6565
> : ^^^^^^^^^^^^^^^^^^^^
6666

67+
import type config2Type from "./config.json"; // Ok, type-only
68+
>config2Type : any
69+
> : ^^^
70+
71+
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
72+
>config2Type2 : any
73+
> : ^^^
74+
>type : any
75+
> : ^^^
76+
6777
import { version } from "./config.json" with { type: "json" }; // Error, named import
6878
>version : number
6979
> : ^^^^^^

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
/main.mts(2,22): error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
44
/main.mts(3,19): error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
55
/main.mts(7,21): error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
6-
/main.mts(8,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'NodeNext'.
7-
/main.mts(10,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
6+
/main.mts(9,47): error TS2857: Import attributes cannot be used with type-only imports or exports.
7+
/main.mts(10,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'NodeNext'.
8+
/main.mts(12,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
89

910

1011
==== /node_modules/not.json/package.json (0 errors) ====
@@ -41,7 +42,7 @@
4142
"version": 1
4243
}
4344

44-
==== /main.mts (5 errors) ====
45+
==== /main.mts (6 errors) ====
4546
import { oops } from "not.json"; // Ok
4647
import moreOops from "actually-json"; // Error in nodenext
4748
~~~~~~~~~~~~~~~
@@ -55,6 +56,10 @@
5556
import config2 from "./config.json"; // Error in nodenext, no attribute
5657
~~~~~~~~~~~~~~~
5758
!!! error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
59+
import type config2Type from "./config.json"; // Ok, type-only
60+
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
61+
~~~~~~~~~~~~~~~~~~~~~
62+
!!! error TS2857: Import attributes cannot be used with type-only imports or exports.
5863
import { version } from "./config.json" with { type: "json" }; // Error, named import
5964
~~~~~~~
6065
!!! error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'NodeNext'.

‎tests/baselines/reference/nodeModulesJson(module=nodenext).symbols

+10-4
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,24 @@ import { default as config1 } from "./config.json" with { type: "json" }; // Ok
4242
import config2 from "./config.json"; // Error in nodenext, no attribute
4343
>config2 : Symbol(config2, Decl(main.mts, 6, 6))
4444

45+
import type config2Type from "./config.json"; // Ok, type-only
46+
>config2Type : Symbol(config2Type, Decl(main.mts, 7, 6))
47+
48+
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
49+
>config2Type2 : Symbol(config2Type2, Decl(main.mts, 8, 6))
50+
4551
import { version } from "./config.json" with { type: "json" }; // Error, named import
46-
>version : Symbol(version, Decl(main.mts, 7, 8))
52+
>version : Symbol(version, Decl(main.mts, 9, 8))
4753

4854
import * as config3 from "./config.json" with { type: "json" };
49-
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
55+
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
5056

5157
config3.version; // Error
52-
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
58+
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
5359

5460
config3.default; // Ok
5561
>config3.default : Symbol("/config")
56-
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
62+
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
5763
>default : Symbol("/config")
5864

5965
=== /loosey.cts ===

‎tests/baselines/reference/nodeModulesJson(module=nodenext).types

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ import config2 from "./config.json"; // Error in nodenext, no attribute
6464
>config2 : { version: number; }
6565
> : ^^^^^^^^^^^^^^^^^^^^
6666

67+
import type config2Type from "./config.json"; // Ok, type-only
68+
>config2Type : any
69+
> : ^^^
70+
71+
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
72+
>config2Type2 : any
73+
> : ^^^
74+
>type : any
75+
> : ^^^
76+
6777
import { version } from "./config.json" with { type: "json" }; // Error, named import
6878
>version : number
6979
> : ^^^^^^

‎tests/cases/conformance/node/nodeModulesJson.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import typed from "actually-json/typed"; // Error in nodenext
4444
import config from "./config.json" with { type: "json" }; // Ok
4545
import { default as config1 } from "./config.json" with { type: "json" }; // Ok
4646
import config2 from "./config.json"; // Error in nodenext, no attribute
47+
import type config2Type from "./config.json"; // Ok, type-only
48+
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
4749
import { version } from "./config.json" with { type: "json" }; // Error, named import
4850
import * as config3 from "./config.json" with { type: "json" };
4951
config3.version; // Error

0 commit comments

Comments
 (0)