microsoft / microsoft/TypeScript
[Feature] Utility type for T[keyof T] or new keyword valueof
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Search Terms
typeof keyof utlity type
Suggestion
Alternative 1:
Create a new Utility type expressing T[keyof T]
Example Value<T> = T[keyof T].
Alternative 2:
Create a new keyword equivalent to T[keyof T]
Example valueof T = T[keyof T]
Use Cases
While T[keyof T] is short and concise, from my experience a lot of developers new to TypeScript usually have no idea what this means or why it's written like that when they first see it.
I also feel that it's not as easy to Google a the answer to that, in comparison to established utility and advanced types like Pick, Omit, Record, etc. which all have their own entries in the TS documentation.
Additionally Object.keys and Object.values are similar concepts.
The presence of a keyof keyword would suggest a valueof should also exist, yet it's missing and one must employ a "special" approach to replicate.
A simple solution is to write my own custom type for the project, but that just introduces additional abstraction and the knowledge/practice isn't transferable to other projects, without implementing the same custom type somewhere in the code first.
And lastly, and this is not a big issue, but I feel it's redundant having to write the type for T twice in on the same line just a few symbols apart.
With short names such as T it's okay, but with longer type names the code becomes more cumbersome to read.
Examples
type T = {
a: string;
b: number
c: number[]
...etc
}
// Value<T> = string | number | number[]...
// valueof T = string | number | number[]...
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.
Perhaps some conflict on point 8...
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ファイル、テスト、エントリーポイントは指定されていません。まず、提案されているユーティリティ型 Value<T> とキーワード valueof T の代替案を確認し、そのうえで、どちらを選ぶべきか判断するために必要な言語設計とコンパイラーのスコープを決定してください。完了とは、設計が確定し、実装計画が策定されている状態を指しますが、この issue では受け入れテストは定義されていません。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100