Skip to content

TS7.1 importing .json with type: something still parses the JSON file even though ambient module declaration is used #65098

Open

Description

🔎 Search Terms

json with

🕗 Version & Regression Information

  • This only happens in 7.1 since it import attributes in ambient modules were added then

⏯ Playground Link

https://github.com/Knagis/ts7-import-json-with

💻 Code

// src/types.d.ts
declare module "*.json" with { type: "text" } {
    const content: string;
    export default content;
}

// src/attribute.ts
import text from "./big.json" with { type: "text" };
const content: string = text; // the type comes from the declaration above, not from the JSON

🙁 Actual behavior

Compiler parses the .json file in memory, but does not use the type anywhere - the ambient module declaration is used.

🙂 Expected behavior

The .json file is not parsed, it might not even be a valid .json file - as noted by with { type: "text" }

Additional information about the issue

No response

Activity

  1. added a commit that references this issue on Nov 2, 2026
    ad182fe
  2. added
    Suggestion
    and removed
    Bug
    on Nov 20, 2026
  3. gabritto commented on Nov 20, 2026

    @gabritto
    Member

    I don't think this behavior is new, and it's intentional. During program construction, we parse big.json because we see it's imported, and we can't be sure it will resolve to an ambient module until later. This was already the case before #63931.
    It would be tricky to decide, during program construction, whether an import specifier like "./big.json" will resolve to that file, or to an ambient module. At the point we come across that import, IIUC it's not guaranteed that we'll have the complete list of all ambient modules that could affect resolution of that specifier. And even if by any chance we happen to know that "./big.json" will resolve to an ambient module at the time we come across it, that's an opportunistic optimization that would mean the files parsed and included in a program depend on the order in which we process files, which feels very weird too. After #63931, this is also complicated by the fact we use type assignability to decide whether a specifier resolves to an ambient module with attributes, and I think it would be possible, though undesirable, to replicate that algorithm for import attributes during program construction.

  4. Knagis commented on Nov 20, 2026

    @Knagis
    ContributorAuthor

    perhaps then the right approach is to reconsider if import foo from "./foo.json" with { type: "not-json" } should ever parse the json file? since the JS spec does say that json import must have { type: "json" }? Though of course, for bundlers we can configure it however we want, it is just browser/node that won't support it with different type...

  5. gabritto commented on Nov 20, 2026

    @gabritto
    Member

    perhaps then the right approach is to reconsider if import foo from "./foo.json" with { type: "not-json" } should ever parse the json file? since the JS spec does say that json import must have { type: "json" }? Though of course, for bundlers we can configure it however we want, it is just browser/node that won't support it with different type...

    This would have to be a config option because it changes by host. Like you said, Node requires the import attribute, browsers too, but that's not required by the ECMA spec, and bundlers can do something else entirely.
    Couple questions: would it solve your issue if you could set resolveJsonModule: false but still import from .json files if there is a with { "type": "json" }?
    Also, what's the total impact of this problem in your actual codebase? I saw the numbers from the repro you posted, but that's for the minimal example contained there.

  6. added
    Possible Improvement
    on Nov 20, 2026
  7. Knagis commented on Nov 20, 2026

    @Knagis
    ContributorAuthor

    yes, setting resolveJsonModule: false but to be able to still import json with explicit type:json would definitely solve my issue. but i suppose that also isn't backwards compatible option, it would have to be something like resolveJsonModule: "strict" that would require type: json for json import and everything else would then be resolved based on ambient module types.

    our actual impact was quite high - i don't remember the number right now but it was significant enough that for the affected projects i went back to using our old import url from "./foo.json?download" approach (which i don't like because no IDE support go to definition on it to open the actual file.

    our usecase is games - and while we do import some/most .json files as data, that is case by case basis - some files are large spritesheets or animations in .json format - that we have no need to parse as json at compile time. of course, i could change the extension for them, but then i will get complaints from designers that they can't simply export the stuff from their tools and they need extra steps... And we actually have some instances where the devs have decided to do import type foo from "./foo.json" with { type: "json" }; import fooUrl from "./foo.json" with { type: "download" } - a bit strange construct, i am not convinced myself it is the right choice, just that we have stuff like that (this case wouldn't benefit from the perf improvement, but it shows a case where i can't change the extension either).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Possible ImprovementSuggestion

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions