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

Add allowImportingJsExtensions option to prevent incorrect .js imports #61371

Open
6 tasks done
DanielRamosAcosta opened this issue Mar 7, 2025 · 1 comment
Open
6 tasks done

Comments

@DanielRamosAcosta
Copy link

DanielRamosAcosta commented Mar 7, 2025

πŸ” Search Terms

moduleResolution nodenext, import js extension, prevent .js imports, strict module resolution

βœ… Viability Checklist

⭐ Suggestion

Introduce a new compiler option:

"allowImportingJsExtensions": false

When set to false, TypeScript should prevent importing .js extensions unless the corresponding .js file physically exists.

Currently, in projects using "moduleResolution": "nodenext", TypeScript allows importing .js extensions even when there is no actual .js file, leading to confusion and incorrect imports in TypeScript-only projects.

πŸ“ƒ Motivating Example

Consider a TypeScript project using "moduleResolution": "nodenext":

import { something } from "./utils.js"; // Allowed, but utils.js does not exist

Here, TypeScript does not raise an error even though utils.js is not present. This can cause issues in projects that exclusively use .ts files and never generate .js files manually.

With the proposed option:

"allowImportingJsExtensions": false

The above import would result in a TypeScript error unless utils.js is physically present.

πŸ’» Use Cases

  1. Prevent incorrect imports in TypeScript-only projects: This option helps ensure .js imports are only allowed when the actual file exists, reducing accidental misconfigurations.
  2. Stricter enforcement of module resolution: This aligns with "moduleResolution": "nodenext", enforcing explicit file extensions while avoiding implicit assumptions.
  3. Avoid runtime errors: Developers may assume a .js file exists due to TypeScript allowing the import, only to discover at runtime that the file is missing. This feature prevents such issues at compile time.
  4. Current workaround is manual: The only way to enforce this today is by manually checking the filesystem or using linting rules, which is not ideal.
@DanielRamosAcosta
Copy link
Author

Related to #61355

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant