Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Le0Michine opened this issue Dec 9, 2024 · 2 comments · Fixed by #60749
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@Le0Michine
Copy link

Le0Michine commented Dec 9, 2024

🔎 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"]
}
@thewilkybarkid
Copy link

I think the second option in expected behaviour should be:

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

(i.e. with a type)

@thewilkybarkid
Copy link

We've encountered the same issue when upgrading from 5.6.3 to 5.7.2 (PREreview/prereview.org#2130). Making it a regular import leads to an import/no-extraneous-dependencies ESLint failure, as the package we're importing from is only used for its types (as so is a dev dependency).

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 12, 2024
@andrewbranch andrewbranch added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Dec 12, 2024
@andrewbranch andrewbranch added this to the TypeScript 5.7.3 milestone Dec 12, 2024
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants