Module resolution: Named imports not supported with import attributes #59684
Labels
Fix Available
A PR has been opened for this issue
Needs Investigation
This issue needs a team member to investigate its status.
Milestone
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: