microsoft / microsoft/TypeScript

Enables `implements` on abstract classes with protected members (usefull for generic mixins)

Đang mở
#61,161 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
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
⭐ 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
  1. What do you want to use this for?

Easily describing generic mixins return type.

  1. What shortcomings exist with current approaches?

Can't describe protected properties.

  1. 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

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. 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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.