microsoft / microsoft/TypeScript
Could there be a `This` type like the `Self` type in rust ?
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Suggestion
🔍 Search Terms
This, Self, typeof this
✅ 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
For example, let's declare an interface Cloneable, instances of classes implementing Cloneable should be able to clone themselves:
interface Cloneable {
clone(): This
}
The return type of function clone() of interface Cloneable should be the actual type of this, or typeof this in the shown code. However typeof this does not work. It will raise TS2304 Cannot find name 'this', and TS4055 Return type of public method from exported class has or is using private name 'this'.
clone(): Cloneable is not the desired way for this interface, or the below code would pass type check:
class A implements Cloneable {
clone() { return new B() }
}
class B implements Cloneable {
clone() { return new A() }
}
Besides, clone() is not returning this. So clone(): this is not the right way to go. It won't allow this to pass:
class A implements Cloneable {
clone() { return new A(this) }
}
Generics could be a workaround, but it can be annoying for complex cases, and still can be a problem:
class A implements Cloneable<B> {
clone() { return new B() }
}
It does not make any sense to have sth like class A implements Cloneable<B>, but generics allows it.
Allowing typeof this, or This, could be a very useful in that case.
This feature could also help in some other cases, like interface Addable:
interface Addable {
add(that: This): This
}
The argument that of function add must be of the same type of this, so it cannot be declared as add(that: Addable): Addable. It also cannot be add(that: this): this.
It makes sense to use generics,
interface Addable<That> {
add(that: That): That
}
But still, it is not a very good way.
📃 Motivating Example
💻 Use Cases
As discussed above, it is mainly used by interfaces and abstract classes, like interface Cloneable, interface Addable, or maybe interface Serializable and interface Serializer.
The current approaches cannot provide the correct behavior, which is discussed above.
I am using generics, but it is incorrect indeed.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue ではリポジトリのファイルやテストが指定されていません。まず、既存の this 型の挙動と、Issue で参照されている TypeScript の設計目標を確認し、次に Cloneable と Addable の例を、それぞれのジェネリックによる回避策と比較してください。完了とするには、提案された型のセマンティクスを確定し、実装計画を策定する必要があります。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 30/100