microsoft / microsoft/TypeScript
Suggestion: Reference expression type during cast
オープン
まだ誰も着手していません。
Awaiting More Feedback
Suggestion
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Search Terms
cast reference expression type
Suggestion
I would like to be able to reference original type of the expression during cast.
For example:
const o = {
x: { a: 1 } as typeof & { b?: number }
}
// type of o.x is now { a: number; b?: number }
I am not attached to typeof syntax here, can be anything that makes sense.
Use Cases
- Defining optional properties without actually initializing them.
Current approach would require me to do something like:
const o = {
x: { a: 1 } as { a: number; b?: number }
}
which is unweildy and repeats all the properties.
It is possible to do through a function, but it has an overhead and is complicated due to partial generics:
function extend<T>(T data) {
return <TExtra>() => data as T & TExtra;
}
const o = {
x: extend({ a: 1 })<{ b?: number }>()
}
- Adding indexers to object literals:
function map1(key: string) {
return { a: 1, b: 2 }[key] || 3; // does not work due to https://github.com/microsoft/TypeScript/issues/14951
}
function map2(key: string) {
return ({ a: 1, b: 2 } as { [key: string]: typeof[keyof typeof]|undefined })[key] || 3;
}
Examples
See use cases.
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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue では、ソースファイル、テスト、エントリーポイントのいずれも指定されていません。まず提案されているキャスト構文と2つのユースケースを確認し、その後、必要な型システムと構文の設計を判断してください。完了とするには、受け入れられたアプローチと、それに対応する実装範囲が必要です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 30/100