Repository: TypeScript
Issue: Support manually enabling warning for not including file extensions in imports
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most commWhen you have `"moduleResolution": "node16"` and `"module": "node16"` but `"type": "commonjs"`, TypeScript will not warn when not including file extensions in relative imports, but if you have `"type": "module"` it will. Not sure if using `"module": "ES2022"` and similar also enables the warning.

Some projects output both ESM and CJS build as the ESM builds are preferable for bundlers and tree shaking, but CJS builds are still sometimes needed due to tooling that supports ESM badly. And you can't reexport the ESM builds from the CJS ones, only the other way around, but that is harmful for tree shaking of most bundlers.

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: on ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

ESM file extension relative imports node16 warning

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->

When you have `"moduleResolution": "node16"` and `"module": "node16"` but `"type": "commonjs"`, TypeScript will not warn when not including file extensions in relative imports, but if you have `"type": "module"` it will. Not sure if using `"module": "ES2022"` and similar also enables the warning.

Some projects output both ESM and CJS build as the ESM builds are preferable for bundlers and tree shaking, but CJS builds are still sometimes needed due to tooling that supports ESM badly. And you can't reexport the ESM builds from the CJS ones, only the other way around, but that is harmful for tree shaking of most bundlers.

Although compiling dual packages isn't currently convenient or well supported in tsc, you might still use external tools such as tsup to produce them and rely on tsc for type checking only, at that point such a warning is useful to catch such errors.

Regerettably as it stands it kind be quite difficult regardless to create working dual packages as TypeScript doesn't transform imports path and it can be tricky to ge tthe right combination of `"type"`, `"tsconfig"` and file extensions in imports to get everything working correctly for both cases.

Related https://github.com/microsoft/TypeScript/issues/46786

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->
For anyone wanting to be sure to enforce inclusion of file extensions in imports, a new tsconfig setting is available to enable regardless of the combination of `module`, `moduleResolution` and `type` in effect.

## 💻 Use Cases

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->
Authoring dual ESM/CJS packages for node. Currently you have to fiddle around with settings until it works, and check things at runtime, as tsc is unreliable at letting you know/checking that everything is set correctly for this case.