microsoft / microsoft/TypeScript
Mapped Type `[Lhs of Rhs]` Syntax for Array and Tuple Types
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提案されている [Lhs of Rhs] の例を、既存の keyof に基づく mapped types の回避策と、配列およびタプルに対するその動作と比較します。次に、mapped types を制御するコンパイラー領域とテストを特定し、受け入れられる構文、readonly unknown[] 制約、結果として得られるタプル型、および無効な右辺に対する診断を定義します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100