microsoft / microsoft/TypeScript
rewriteRelativeImportExtensions & enforce consistent extensions
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
🔍 Search Terms
"rewriteRelativeImportExtensions", "extensions"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
An option/param to enforce imports with relative paths to point to files existing in sources rather than in built files, in short, make imports only allowing .ts and not .js extensions when importing a Typescript file.
📃 Motivating Example
rewriteRelativeImportExtensions is a great new feature and it works well. It permits to create hybrid projects directly executed by recent nodejs versions, but also buildable by tsc for distributing them. But since we now have the possibility to use the .ts extensions in imports because they will be rewritten at compilation time, we still can continue to use the .js extensions in parallel, pointing to built files.
As everyone knows, nodejs requires relative imports to have an extension and to point to an existing file and so, extensions have to be .ts (or .mts/.cts) to import other Typescript files.
It would be probably great that Typescript gives us an error when using unreachable file in this context, like its quasi-exact opposite: TS5097: An import path can only end with a .ts extension when allowImportingTsExtensions is enabled when .ts extensions are not allowed by configs.
💻 Use Cases
-
What do you want to use this for?
In projects where nodejs can be used for any reason (tests, various scripts) and where code have to be built to be distributed over npm or any other repository, to avoid detecting badimports extensions at nodejs runtime and to keep a consistent code base. -
What shortcomings exist with current approaches?
Only its permittivity, allowing to use inconsistentimports styles, that may don't run in nodejs, without detecting them at coding time. -
What workarounds are you using in the meantime?
It's not a bug, there's no need of workarounds, only vigilance.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 rewriteRelativeImportExtensions 的现有实现和测试,然后将其行为与 allowImportingTsExtensions 以及诊断 TS5097 进行比较。定义应如何验证相对 .js、.ts、.mts 和 .cts 导入,并添加覆盖用例,表明新选项会拒绝无法解析为源文件的导入。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- node.js, typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100