microsoft / microsoft/TypeScript
Allow inference of class generics when declaring an interface
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
Search Terms
interface, inference, extending class
Suggestion
I'd like to be able to strongly type an interface such that when it is used on a class that has a generic I can infer the type of that generic to then use in the declaration of the interface.
Use Cases
I want to use this within a library that exposes both an abstract class and an optional interface that developers can opt-in to use. The abstract class has one generic parameter, and in order to correctly type the interface without duplicate generic declaration I want to be able to infer the abstract class' generic type.
Currently the inheriting class declarations must duplicate their generic type declaration.
This issue is similar to https://github.com/microsoft/TypeScript/issues/26242 I think, but I don't see how the resolution of that issue will fix this one.
Examples
abstract class Base<T> {
// implementation omitted for brevity
}
// ⬇️this is the proposed new syntax ⬇️
interface Remap<T> extends Base<infer U> {
remap(input: U): T;
}
class Foo extends Base<string> implements Remap<number> {
public remap(input: string): number {
return input;
}
}
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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 xác định tệp mã nguồn, bài kiểm thử hoặc các điểm vào của trình biên dịch. Hãy bắt đầu bằng cách đọc cú pháp TypeScript được đề xuất và issue liên quan #26242, sau đó lần theo cách tính kế thừa generic và các khai báo interface hiện đang được kiểm tra. Công việc được xem là hoàn tất khi nhóm đã xác định và triển khai hành vi suy luận mong muốn, kèm theo độ bao phủ cho ví dụ được nêu và các trường hợp biên của nó.
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
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100