microsoft / microsoft/TypeScript
Could there be a `This` type like the `Self` type in rust ?
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 117
描述
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 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 没有列出任何 repository 文件或测试。首先检查现有的 this 类型行为以及 issue 中引用的 TypeScript 设计目标,然后将 Cloneable 和 Addable 示例与它们的泛型变通方案进行比较;要视为完成,需要确定明确的语义,并为提议的类型制定实现计划。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 30/100