microsoft / microsoft/TypeScript
Support forwarding generic type in extending class
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Search Terms
generic forward extends default
Suggestion
Have a shortcut syntax to make the generic of an extending class the same type as its Parent generic.
Current
class Parent <T extends any = any> {}
class Child <S extends any = any> extends Parent <S> {}
Suggestion
class Parent <T extends any = any> {}
class Child <infer S> extends Parent <S> {}
Use Cases
A default generic is useful to typescript users b
interface Proto = {}
class SomeClass <T extends Proto = Proto> {}
It can be extended like this:
class ExtendedClass extends SomeClass {}
but with this the author of ExtendedClass robs futher extenders of the possibility to extend the T generic of SomeClass. To allow further extension the author would need to add following construct.
class ExtendedClass <T extends Proto = Proto> extends SomeClass<T> {}
This is quite bothersome to the author of ExtendedClass. It requires the import of Proto and is easy to forget.
To make it a little bit easier I thought it might be a good idea to have this special syntax.
Examples
type Events = {
open: () => {}
close: () => {}
}
class System <TEvents extends Events = Events> {
on <TEvent extends keyof TEvents> (event: TEvent, listener: TEvents[TEvent]): this { return this }
off <TEvent extends keyof TEvents> (event: TEvent, listener: TEvents[TEvent]): this { return this }
}
class CorpSystem <infer TEvents> extends System <TEvents> {}
type SectionEvents = {
stall: () => {}
} & Events
class SectionSystem <TEvents extends SectionEvents = SectionEvents> extends CorpSystem<TEvents> {}
const s = new SectionSystem()
s.on('stall', () => {})
As probably clear in the example, this thought came up when dealing with Node's EventEmitter but I think it can be useful to pretty much any use of extending classes with generics.
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 にある提案された infer 構文と Parent、Child、System、CorpSystem、SectionSystem の例から始めます。ジェネリックな転送がクラス継承およびデフォルト値とどのように相互作用すべきかを決定します。構文とその型チェック時の動作が仕様化され、適切なコンパイラテストでカバーされれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100