You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TS7.1 importing .json with type: something still parses the JSON file even though ambient module declaration is used #65098
// src/types.d.tsdeclare module "*.json"with{ type: "text"}{constcontent: string;exportdefaultcontent;}// src/attribute.tsimporttextfrom"./big.json"with{type: "text"};constcontent: 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" }
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.
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...
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.
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).
Activity
fix(compiler): skip loading files matched by attributed pattern ambients
gabritto commentedon Nov 20, 2026
I don't think this behavior is new, and it's intentional. During program construction, we parse
big.jsonbecause 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.Knagis commentedon Nov 20, 2026
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...gabritto commentedon Nov 20, 2026
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: falsebut still import from.jsonfiles if there is awith { "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.
Knagis commentedon Nov 20, 2026
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).