microsoft / microsoft/TypeScript
Support either type asserting `self` in classes, or properly ignoring workaround
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ả
I currently have a scenario very similar to https://github.com/microsoft/TypeScript/issues/2000#issuecomment-546929745. While it would be fixed it TS natively supported overriding class methods (see that issue), I followed the outline of the linked comment, but am running into a small issue. This is not a huge blocker but rather a nice-to have to get proper type completion for my scenario.
export class WritableObjStream<T> extends (stream.Writable as any) {
writableObjectMode: true
write(object: T, cb?: (error: Error | null | undefined) => void): boolean {
return super.write(...more details)
}
}
Because we type assert the base class to any, TS has no knowledge of the superclass. I tried a few solutions, with errors included below:
(super as any).write() // 'super' must be followed by an argument list or member access.
let s: stream.Writable = undefined;
// @ts-ignore
s = super // Identifier expected.
return s.write() // 'super' must be followed by an argument list or member access, Expected 1-3 arguments, but got 0.
Luckily, it autocomplete is picking up on the proper type of s.write() now and giving me an argument error.
Also, from what I can gather super is treated as a keyword not an identifier, which results in the error above.
This may not be the most common of errors, but there may be a few scenarios/other use cases. I tried writing a few examples up but none made real-world sense besides this use-case of a workaround for overriding methods.
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
Bắt đầu với ví dụ về class trong issue và issue/comment TypeScript được liên kết mô tả workaround. Điều tra cách TypeScript phân tích cú pháp và kiểm tra kiểu của super trong kịch bản này, sau đó xác định liệu việc type-assert self có nên được hỗ trợ hay workaround nên bị từ chối một cách rõ ràng. Hoàn tất khi hành vi được chọn được ghi nhận bằng các test bao quát những ví dụ đã nêu.
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