microsoft / microsoft/TypeScript
Suggestion: Allow interfaces to "implement" (vs extend) other interfaces
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
interface, implements
Suggestion
Allow declaring that an interface "implements" another interface or interfaces, which means the compiler checks conformance, but unlike the "extends" clause, no members are inherited:
interface Foo {
foo(): void;
}
interface Bar implements Foo {
foo(): void; // must be present to satisfy type-checker
bar(): void;
}
Use Cases
It is very common for one interface to be an "extension" of another, but the "extends" keyword is not a universal way to make this fact explicit in code. Because of structural typing, the fact that one interface is assignable to another is true with or without "extends," so you might say the "extends" keyword serves primarily to inherit members, and secondarily to document and enforce the relationship between the two types. Inheriting members comes with a readability trade-off and is not always desirable, so it would be useful to be able to document and enforce the relationship between two interfaces without inheriting members.
Consider code such as:
import { GestureHandler } from './GestureHandler'
import { DropTarget } from './DropTarget'
export interface DragAndDropHandler extends GestureHandler {
updateDropTarget(dropTarget: DropTaget): void;
}
function createDragAndDropHandler(/*...*/): DragAndDropHandler {
//...
}
While this code is not bad, it is notable that DragAndDropHandler omits some of its members simply because it has a relationship with GestureHandler. What are those members? What if I would like to declare them explicitly, just as I would if GestureHandler didn't exist, or if DragAndDropHandler were a class that implemented GestureHandler? I could write them in, but the compiler won't check that I have included all of them. I could omit extends GestureHandler, but then the type-checking will happen where DragAndDropHandler is used as a GestureHandler, not where it is defined.
What I really want to do is be explicit about — and have the compiler check — that I am specifying all members of this interface, and also that it conforms to GestureHandler.
I would like to be able to write:
export interface DragAndDropHandler implements GestureHandler {
updateDropTarget(dropTarget: DropTaget): void;
move(gestureInfo: GestureInfo): void
finish(gestureInfo: GestureInfo, success: boolean): void
}
Examples
See above
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. new expression-level syntax)
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. Hãy bắt đầu bằng cách lần theo cách compiler xử lý các khai báo interface extends và xem lại phần thảo luận để tìm thiết kế đã được thống nhất; sau đó xác định tính tương thích mà không có kế thừa member. Thêm các bài kiểm thử compiler bao quát các ví dụ và xác minh rằng cú pháp mới kiểm tra các member bắt buộc mà không thay đổi JavaScript được phát ra.
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
- 35/100