microsoft / microsoft/TypeScript
Could there be a `This` type like the `Self` type in rust ?
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Issue không nêu tên tệp hoặc bài kiểm thử nào trong repository. Hãy bắt đầu bằng việc xem xét hành vi hiện tại của kiểu this và các mục tiêu thiết kế TypeScript được tham chiếu trong issue, sau đó so sánh các ví dụ Cloneable và Addable với các cách workaround generic tương ứng; để được coi là hoàn tất, cần xác định rõ ngữ nghĩa và có kế hoạch triển khai cho kiểu được đề xuất.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 30/100