microsoft / microsoft/TypeScript
Enables `implements` on abstract classes with protected members (usefull for generic mixins)
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ả
🔍 Search Terms
implement abstract class with protected
✅ Viability Checklist
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Enable doing the following (currently this raises an error):
abstract class Base {
protected abstract foo(): void
}
class B implements Base {
protected foo() {}
}
📃 Motivating Example
This would be useful to declare "protected" interfaces, when we can't use extends as the child class is already extending another.
// interface
abstract class Base {
protected abstract foo(): void
}
class C {}
class B extends C implements Base {
protected foo() {}
}
Such need is frequent when using generic mixins: indeed, type manipulations inside generic functions (and mixins) are often quite troublesome, with unwanted type expansions/generalization (?). Then this is often better explicitly describe the returning type.
To do so, abstract class is the only way to properly describe the properties added by the mixin. However, if such class has protected properties, this raises TS error as classes with protected properties are no longer duck typing but "nominal" (?).
type Cstr<T> = new(...args:any[]) => T;
type Merge<A extends abstract new(...args:any[])=>any,
B extends new(...args:any[])=>any
> = Omit<A & B, "new"> & (new() => (InstanceType<A> & InstanceType<B>))
function foo<T extends Cstr<C>>(klass: T): Merge<typeof Base, T> {
return class B extends klass implements Base {
protected foo() {}
}
}
class X extends foo(C) {}
💻 Use Cases
- What do you want to use this for?
Easily describing generic mixins return type.
- What shortcomings exist with current approaches?
Can't describe protected properties.
- What workarounds are you using in the meantime?
Dirty casting, removing some type checks inside the generic mixin.
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 cung cấp đường dẫn tệp hoặc test nào. Hãy bắt đầu bằng cách xác định logic kiểm tra kiểu và các test về khả năng tương thích của class có implements với các member protected, sau đó so sánh các ví dụ về abstract class và generic mixin. Công việc được xem là hoàn tất khi các ví dụ vượt qua kiểm tra kiểu mà không thay đổi JavaScript được phát ra, kèm theo coverage hồi quy.
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
- 35/100