`"module": "node16"` should support extension rewriting
Problem
Bug Report TypeScript 4.7 RC introduces the `"module": "node16"` setting. When used, this requires users to use the `.js` extension in import paths. I strongly believe this to be a mistake and TypeScript should instead allow users to use the `.ts`/`.tsx` extension of the source file. It would then be the TS compiler’s responsibility to rewrite this to the output file as necessary. I have several reasons for requesting this change: - The file with the `.js` extension most likely does not exist on disk. It may exist on disk if a build has been created and the output is placed right alongside the sources (not a common configuration), but otherwise the file is simply not there or in a different place than one might expect. This is confusing to users, because they need to manually consider how output files are created. - Using the `.js` extension makes the source code less portable for full-stack projects. If a user wants to use the same sources for a frontend project that uses a bundler, they will be out of luck. A bundler would be able to understand a reference to the `.ts` file (because that one actually exists on disk!), but would struggle with references to files that do not exist for its configuration. - Using the `.js` extension makes the source incompatible with Deno projects, as they use the `.ts`/`.tsx` extensions, because those are the ones that actually exist. - Using the `.js` extension is inconsistent with type import. Does `import type { MyType } from “./myFile.j
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: `"module": "node16"` should support extension rewriting
Thanks for your reply. I understand I’m getting at some pretty fundamental issues here, so I try to navigate this as delicately as I can. First, the new thing I mentioned is the `node16` module resolution mechanism that comes with TypeScript 4.7. Unlike previous module resolutions supported by Typescript, this forces users to use the `.js` extension in their paths. While this may be a consequence
Trust Score
9 verifications
- 1
Thanks for your reply. I understand I’m getting at some pretty fundamental issue
Thanks for your reply. I understand I’m getting at some pretty fundamental issues here, so I try to navigate this as delicately as I can.
- 2
First, the new thing I mentioned is the `node16` module resolution mechanism tha
First, the new thing I mentioned is the `node16` module resolution mechanism that comes with TypeScript 4.7. Unlike previous module resolutions supported by Typescript, this forces users to use the `.js` extension in their paths. While this may be a consequence of decisions that were made earlier, this does exacerbate the issues that result from those decisions. If nothing else, I believe that may present a valuable moment to reflect on those decisions to see if they still achieve the goals you set out to achieve.
- 3
I also believe the timing is important here. So far, and as far as I’m aware, Ty
I also believe the timing is important here. So far, and as far as I’m aware, Typescript users have mostly avoided including extensions in their paths. And for good reason: Including them makes their code less compatible with existing bundlers used in the ecosystem. One notable exception here are Deno users, who include the `.ts` extension, in a resolution mechanism that follows the ESM resolution in spirit.
- 4
If TypeScript 4.7 is released with the `node16` setting as it’s implemented toda
If TypeScript 4.7 is released with the `node16` setting as it’s implemented today, I’m afraid this may introduce further schism between the various user groups. This is unfortunate, because `node16` is presented as the way to do ESM with TypeScript, but for users to use it they will be forced to move away from their current practices in a manner that is incompatible with their current tools. It will become harder for a single codebase to support Node.js and bundlers, while there is no technical reason for this to be so.
Validation
Resolved in microsoft/TypeScript GitHub issue #49083. Community reactions: 115 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep