-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Module Resolution: imports with JSON assertion should not need flag when module is esnext #54929
Comments
Both of these should be You won't need
Any command line options can be put into // tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"resolveJsonModule": true
}
}
At present JSON modules are still experimental in Node and Browsers, in fact if you're using import assertions without transpilation this might break in future as the proposal is being changed to replace |
ASDI addressed above; I don't understand why you're saying it's a bug that you have to specify |
Thank you, I did not know
@RyanCavanaugh because, albeit warning as being an experimental feature, import assertions work by default since node 16. Why hide it behind a flag in TS (given appropriate Granted, as said above, this feature may change. So do new TS5 decorators, but they do not require a flag. They actually only work with the previous I suppose you'd close this issue if you think that the RJM flag remark is not really a bug. |
One particular difference is that decorators can be transpiled away by choosing a different target, however import assertions cannot be. Using import assertions requires knowing that the host will actually support the feature, it cannot be transpiled away. To be honest though I do think it would make sense for
If you're using import assertions in current Node then you are currently getting the following warnings printed:
These warnings are serious, as mentioned if it's sufficiently web compatible the |
The current behavior is that import assertions don't change the behavior of module resolution, which makes it more consistent. We could make it less consistent and say that certain assertions change certain behavior, but I'd like to see a proposal what sorts of assertions should cause which sorts of changes |
Demo Repo
https://github.com/tminuscode/resolve-json-module
Which of the following problems are you reporting?
The module specifier resolves at runtime, but not at build time
Demonstrate the defect described above with a code sample.
import htmlTags from 'html-tags/html-tags.json' assert { type: 'json' }
const keys = Object.keys(htmlTags)
Run
tsc --showConfig
and paste its output here{
"compilerOptions": {
"target": "es2020",
"module": "esnext",
"moduleResolution": "node16"
},
"files": [
"./index.ts"
]
}
Run
tsc --traceResolution
and paste its output herehttps://pastebin.com/GzfXrXi4
Sorry, content too long
Paste the
package.json
of the importing module, if it exists{
"name": "resolve-json-module",
"dependencies": {
"html-tags": "^3.3.1",
"typescript": "^5.1.6"
}
}
Paste the
package.json
of the target module, if it exists{
"name": "html-tags",
"version": "3.3.1",
"description": "List of standard HTML tags",
"license": "MIT",
"repository": "sindresorhus/html-tags",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts",
"void.js",
"void.d.ts",
"html-tags.json",
"html-tags-void.json"
],
"keywords": [
"html",
"html5",
"tags",
"elements",
"list",
"whatwg",
"w3c",
"void",
"self-closing"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"xo": {
"rules": {
"import/extensions": "off"
}
}
}
Any other comments can go here
I wan to use the latest ES feature of asserted imports to directly load a JSON from the package
html-tags
. The feature requires targetingesnext
and I expected it to be enough, given the no compilation actually happens - the syntax is exactly the same in TS and JSHowever, to actually make
tsc
happy, it is necessary to add--resolveJsonModule --allowSyntheticDefaultImports
. Without these two, I'm getting these errorsresolveJsonModule
seems completely redundant because importing JSON is supported perassert { type: 'json' }
I'm not sure about this. Isn't default-import the only way to import a JSON file?
The text was updated successfully, but these errors were encountered: