microsoft / microsoft/TypeScript
Restrict template literal interpolation expressions to strings
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
Search Terms
"template literal"
There are many hits, some which seem related, but everything I could find was a much bigger ask or wider in scope
Suggestion
Add a compiler option to enforce using only strings in ES6 string template literals
Use Cases
When using string literals, any variables are coerced to strings, which can lead to undesirable behavior.
As far as I can tell there's no way to avoid this behaviour. In the spirit of tying everything, I'd prefer that only actual string types are permissible for use in string templates to avoid accidental coercion by passing null, undefined or object types that may have unexpected string representations, and force users to explicitly convert them to strings.
Examples
For example:
function formatName(name: string | null): string {
return `Name is: ${name}`;
}
formatName(null) === "Name is: null"
Ideally the compiler would fail since name can be null.
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
Issue 中没有列出文件、测试或编译器入口点。先检查模板字面量类型检查行为和评论中的讨论,然后在实现之前定义该选项接受的表达式类型、诊断信息和测试覆盖范围。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100