Do not require import attribute on type-only JSON import (#60749)

This commit is contained in:
Andrew Branch
2024-12-12 11:42:25 -08:00
committed by GitHub
parent 6a00bd2422
commit 44e84bb7b0
8 changed files with 61 additions and 17 deletions
+1 -1
View File
@@ -48122,7 +48122,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
if (moduleKind === ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
if (!importClause.isTypeOnly && moduleKind === ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
// Import attributes/assertions are not allowed in --module node16, so don't suggest adding one
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]);
}
@@ -2,10 +2,11 @@
/loosey.cts(6,9): error TS2339: Property 'default' does not exist on type '{ version: number; }'.
/main.mts(5,36): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(6,52): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/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'.
/main.mts(8,41): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(9,42): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(10,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
/main.mts(9,47): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/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'.
/main.mts(10,41): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(11,42): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(12,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
==== /node_modules/not.json/package.json (0 errors) ====
@@ -42,7 +43,7 @@
"version": 1
}
==== /main.mts (6 errors) ====
==== /main.mts (7 errors) ====
import { oops } from "not.json"; // Ok
import moreOops from "actually-json"; // Error in nodenext
import typed from "actually-json/typed"; // Error in nodenext
@@ -54,6 +55,10 @@
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
import config2 from "./config.json"; // Error in nodenext, no attribute
import type config2Type from "./config.json"; // Ok, type-only
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
import { version } from "./config.json" with { type: "json" }; // Error, named import
~~~~~~~
!!! error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.
@@ -42,18 +42,24 @@ import { default as config1 } from "./config.json" with { type: "json" }; // Ok
import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : Symbol(config2, Decl(main.mts, 6, 6))
import type config2Type from "./config.json"; // Ok, type-only
>config2Type : Symbol(config2Type, Decl(main.mts, 7, 6))
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : Symbol(config2Type2, Decl(main.mts, 8, 6))
import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : Symbol(version, Decl(main.mts, 7, 8))
>version : Symbol(version, Decl(main.mts, 9, 8))
import * as config3 from "./config.json" with { type: "json" };
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
config3.version; // Error
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
config3.default; // Ok
>config3.default : Symbol("/config")
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
>default : Symbol("/config")
=== /loosey.cts ===
@@ -64,6 +64,16 @@ import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : { version: number; }
> : ^^^^^^^^^^^^^^^^^^^^
import type config2Type from "./config.json"; // Ok, type-only
>config2Type : any
> : ^^^
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : any
> : ^^^
>type : any
> : ^^^
import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : number
> : ^^^^^^
@@ -3,8 +3,9 @@
/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'.
/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'.
/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'.
/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'.
/main.mts(10,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
/main.mts(9,47): error TS2857: Import attributes cannot be used with type-only imports or exports.
/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'.
/main.mts(12,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
==== /node_modules/not.json/package.json (0 errors) ====
@@ -41,7 +42,7 @@
"version": 1
}
==== /main.mts (5 errors) ====
==== /main.mts (6 errors) ====
import { oops } from "not.json"; // Ok
import moreOops from "actually-json"; // Error in nodenext
~~~~~~~~~~~~~~~
@@ -55,6 +56,10 @@
import config2 from "./config.json"; // Error in nodenext, no attribute
~~~~~~~~~~~~~~~
!!! error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
import type config2Type from "./config.json"; // Ok, type-only
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2857: Import attributes cannot be used with type-only imports or exports.
import { version } from "./config.json" with { type: "json" }; // Error, named import
~~~~~~~
!!! error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'NodeNext'.
@@ -42,18 +42,24 @@ import { default as config1 } from "./config.json" with { type: "json" }; // Ok
import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : Symbol(config2, Decl(main.mts, 6, 6))
import type config2Type from "./config.json"; // Ok, type-only
>config2Type : Symbol(config2Type, Decl(main.mts, 7, 6))
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : Symbol(config2Type2, Decl(main.mts, 8, 6))
import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : Symbol(version, Decl(main.mts, 7, 8))
>version : Symbol(version, Decl(main.mts, 9, 8))
import * as config3 from "./config.json" with { type: "json" };
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
config3.version; // Error
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
config3.default; // Ok
>config3.default : Symbol("/config")
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
>default : Symbol("/config")
=== /loosey.cts ===
@@ -64,6 +64,16 @@ import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : { version: number; }
> : ^^^^^^^^^^^^^^^^^^^^
import type config2Type from "./config.json"; // Ok, type-only
>config2Type : any
> : ^^^
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : any
> : ^^^
>type : any
> : ^^^
import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : number
> : ^^^^^^
@@ -44,6 +44,8 @@ import typed from "actually-json/typed"; // Error in nodenext
import config from "./config.json" with { type: "json" }; // Ok
import { default as config1 } from "./config.json" with { type: "json" }; // Ok
import config2 from "./config.json"; // Error in nodenext, no attribute
import type config2Type from "./config.json"; // Ok, type-only
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
import { version } from "./config.json" with { type: "json" }; // Error, named import
import * as config3 from "./config.json" with { type: "json" };
config3.version; // Error