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
Paste the package.json of the target module, if it exists
N/A
Any other comments can go here
TypeScript 5.3 added support for import attributes. tsc does not warn or error when attempting to use named imports for these kinds of imports. When named imports are used, they are preserved in the produced .js and there is no environment where they are supported.
Given that --resolveJsonModule has historically allowed named imports and produces valid CommonJS, it's likely that developers will assume TypeScript can handle named imports when using import attributes as well.
For example, if the linked demo repository is set to output CommonJS instead of ESM, then:
It seems that TypeScript should disallow named imports when import attributes are used, or should compile so that a default import is used, similar to handling in CommonJS.
The text was updated successfully, but these errors were encountered:
mdmower
changed the title
Module resolution:
Module resolution: Named imports not supported with import attributes
Aug 19, 2024
NB: I'm not a module expert. I'd assume import attributes are not the issue here since TS is intentionally agnostic to them. But if you're using esm and importing a json module, I'd think #46434 would warn on named imports. That doesn't seem to be happening; why not?
Demo Repo
https://github.com/mdmower/with-json-tsc
See bottom of post for information.
Which of the following problems are you reporting?
The module specifier resolves at build time, but not at run time
Demonstrate the defect described above with a code sample.
import {compilerOptions} from './tsconfig.json' with {type: 'json'};
Run
tsc --showConfig
and paste its output hereRun
tsc --traceResolution
and paste its output hereFound 'package.json' at 'C:/Users/matt.mower/source/with-json-tsc/package.json'.
Paste the
package.json
of the importing module, if it existsPaste the
package.json
of the target module, if it existsN/A
Any other comments can go here
TypeScript 5.3 added support for import attributes.
tsc
does not warn or error when attempting to use named imports for these kinds of imports. When named imports are used, they are preserved in the produced.js
and there is no environment where they are supported.For example, in the linked demo repository:
compiles to:
which does not run in Node.js 22 or Chrome 127.
Given that
--resolveJsonModule
has historically allowed named imports and produces valid CommonJS, it's likely that developers will assume TypeScript can handle named imports when using import attributes as well.For example, if the linked demo repository is set to output CommonJS instead of ESM, then:
compiles to:
It seems that TypeScript should disallow named imports when import attributes are used, or should compile so that a default import is used, similar to handling in CommonJS.
The text was updated successfully, but these errors were encountered: