microsoft / microsoft/TypeScript

Add allowImportingJsExtensions option to prevent incorrect .js imports

未关闭
#61,371 1 条评论 8 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先检查编译器选项是如何定义的,以及 moduleResolution nodenext 如何解析 .js 导入。确定缺少和已有 .js 文件时的预期行为,然后为所提议的选项添加有针对性的测试覆盖。完成的标准是该选项已有文档说明和验证,并且其行为已由测试覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。