microsoft / microsoft/TypeScript
Mapped Type `[Lhs of Rhs]` Syntax for Array and Tuple Types
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.4k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 117
描述
Suggestion
🔍 Search Terms
mapped type, of, array, tuple
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
A new [Lhs of Rhs] syntax in mapped type to map over values of array and tuple types:
type ResultType = {
[V of SourceType]: DoSomethingWith<V>
}
Rhs does not need to be a type parameter.
Rhs must extend readonly unknown[] or a type error is raised.
📃 Motivating Example
type Foo = ['hello', 'world']
type Bar = {
[V of Foo]: V[]
}
// Bar = ['hello'[], 'world'[]]
💻 Use Cases
Currently, to do the same as in motivating example, one might intuitively write:
type Bar = {
[K in keyof Foo]: Foo[K][]
}
However this is incorrect and is a mistake that's potentially difficult to spot.
Instead one must either introduce a generic intermediate type:
type Transform<T> = {
[K in keyof T]: T[K][]
}
type Bar = Transform<Foo>
Or use the infer workaround:
type Bar = Foo extends infer T ? {
[K in keyof T]: T[K][]
} : never
Both workarounds are not immediately obvious to inexperienced developers, less ergonomic, and will silently break when Foo is no longer an array or tuple type without putting extra constraints.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,将提议的 [Lhs of Rhs] 示例与现有的基于 keyof 的 mapped types 变通方案及其对数组和元组的行为进行比较。然后确定控制 mapped types 的编译器区域和测试,并定义可接受的语法、readonly unknown[] 约束、生成的元组类型,以及针对无效右侧的诊断信息。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100