microsoft / microsoft/TypeScript
Suggestion: use receiver's declared type arguments as defaults to its class constructor
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
I run into this relatively simple pattern a lot:
class GenericClass<T, U, V> {
a: T;
b: U;
c: V;
}
let x: GenericClass<number, string, boolean>;
x = new GenericClass();
// Which produces the error:
// Error: GenericClass<{}, {}, {}> is not assignable to GenericClass<number, string, boolean>
Since x has already been defined with a particular set of type arguments, these arguments could be implicitly assumed as defaults to its type's constructor when invoked with it being the receiver (for simplicity I'm assuming here it is the exact constructor as declared e.g. not a derived class).
I understand there will be cases where that would be very difficult or even impossible to achieve:
let createInstance = () => new GenericClass()
x = createInstance();
but these are relatively rare. Most of the time it's just:
class Example {
myMap: Map<string, Array<{n: number}>>;
constructor() {
this.myMap = new Map<string, Array<{n: number}>>(); // <-- Why repeat?
}
}
Which instead could have been:
class Example {
myMap: Map<string, Array<{n: number}>>;
constructor() {
this.myMap = new Map(); // <-- Better.. :)
}
}
- Any possible issues with this? perhaps ones that I'm not aware of or haven't thought of?
- Potential name collisions? (though I'm not aware of any?)
- Maybe just a bit too difficult to implement? perhaps revisit later? (though #5256 appears more difficult than this and seems to be in consideration?)
- Or maybe this 'bends' the semantics of the constructor expression (
new Class<T>()), or at least its default type arguments, in a way that's slightly unappealing from a "purist" point of view"? (though this doesn't seem like a "purist" language?). Perhaps some concept of "contextual baseline default type arguments" can be included as an integral part of the future proposal for default type arguments?
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ファイル、テスト、またはコンパイラーのエントリーポイントは指定されていません。まず GenericClass と Map の例を確認し、次に受け取り側の式からコンストラクターの型引数がどのように推論されるかを追跡してください。コンテキストに基づくデフォルト値、間接呼び出し、派生クラス、コンストラクターのセマンティクスについて扱いが確定すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100