microsoft / microsoft/TypeScript

Add allowImportingJsExtensions option to prevent incorrect .js imports

オープン
#61,371 コメント 1 件 リアクション 8 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

### 🔍 Search Terms

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

### ✅ Viability Checklist

- [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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

Introduce a new compiler option:

```json
"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"`:

```ts
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:

```json
"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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、コンパイラオプションがどのように定義されているか、また moduleResolution nodenext が .js インポートをどのように解決するかを確認します。存在しない .js ファイルと存在する .js ファイルに対する意図した動作を判断し、その後、提案されたオプションについて集中的なテストカバレッジを追加します。オプションがドキュメント化され、検証され、その動作がテストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。