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
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
Prevent incorrect imports in TypeScript-only projects: This option helps ensure .js imports are only allowed when the actual file exists, reducing accidental misconfigurations.
Stricter enforcement of module resolution: This aligns with "moduleResolution": "nodenext", enforcing explicit file extensions while avoiding implicit assumptions.
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.
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.
The text was updated successfully, but these errors were encountered:
π Search Terms
moduleResolution nodenext, import js extension, prevent .js imports, strict module resolution
β Viability Checklist
β Suggestion
Introduce a new compiler option:
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"
: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:
The above import would result in a TypeScript error unless
utils.js
is physically present.π» Use Cases
.js
imports are only allowed when the actual file exists, reducing accidental misconfigurations."moduleResolution": "nodenext"
, enforcing explicit file extensions while avoiding implicit assumptions..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.The text was updated successfully, but these errors were encountered: