Skip to content

Importing JSON file as type only conflicts with import attribute type requirement #60724

Closed
@Le0Michine

Description

@Le0Michine

🔎 Search Terms

"import attributes", "import assert", "type json"

🕗 Version & Regression Information

  • This changed between versions 5.3.3 and 5.7.2
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about import attributes and type only imports
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Couldn't get playground to create a separate module, so attaching a minimal example
typescript-json-repro.zip

run

npm i
npm run build

💻 Code

// tsc fails with: Importing a JSON file into an ECMAScript module requires a 'type: "json"'
import type enMessages from "./en.json";

🙁 Actual behavior

When I try to address the issue as TSC suggests I run into another:

// tsc fails with: Importing a JSON file into an ECMAScript module requires a 'type: "json"'
import type enMessages from "./en.json";

// tsc fails with: Import attributes cannot be used with type-only imports or exports. ts(2857)
import type enMessages from "./en.json" with { type: "json" };

// tsc is happy but me and eslint are not, I don't use imported module for anything other than typings in this case
import enMessages from "./en.json" with { type: "json" };

🙂 Expected behavior

Type only imports should not require specifying 'type: "json"' as they are not retained in the JS output. Alternatively import attributes should be allowed with type only imports. One of the options below should work

import type enMessages from "./en.json";
import enMessages from "./en.json" with { type: "json" };

Additional information about the issue

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "esModuleInterop": true,
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "resolveJsonModule": true,
    "declaration": true,
    "rootDir": "src",
    "outDir": "dist/types"
  },
  "include": ["./src/**/*", "./src/**/*.json"],
  "exclude": ["build", "node_modules", "dist"]
}

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions