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

Dynamically importing JSON should require import attribute with node16/nodenext #60598

Open
kirkwaiblinger opened this issue Nov 25, 2024 · 1 comment
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@kirkwaiblinger
Copy link

kirkwaiblinger commented Nov 25, 2024

πŸ”Ž Search Terms

dynamic import, import attribute, resolveJsonModule,

πŸ•— Version & Regression Information

⏯ Playground Link

https://github.com/kirkwaiblinger/repro-TS-dynamic-json-import-validation

πŸ’» Code

// module: node16/nodenext, resolveJsonModule
// .cts or .mts, it doesn't matter.

async function main() {
    const somethingDynamic = await import('./someThing.json');
    console.log('dynamically imported JSON:', somethingDynamic);
}

main();

export {}

πŸ™ Actual behavior

No error, even though this is a runtime error in nodejs.

πŸ™‚ Expected behavior

Error because dynamic import('./someThing.json') requires import attribute.

Additional information about the issue

Unlike the static import case, this is the case for both commonjs and esm outputs, since import() is available in commonjs modules in node and has the same semantics there as the ESM import (and therefore TS doesn't transform the import() statement to a Promise.resolve(require())).

@kirkwaiblinger
Copy link
Author

Note - obviously not all dynamic imports even can be analyzed, but TS does already analyze these sorts of dynamic imports where a literal is provided to both a) provide type definitions in the file, and b) copy the JSON file to the output dist. If it's doing that, I think it should also demand the import attributes (whereas obviously I wouldn't expect this for import(arbitraryString)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants