microsoft / microsoft/TypeScript
Parameters<T> and ReturnType<T> limitation
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ả
TypeScript Version: 3.9.2
Search Terms:
Generic Method Extend Parameters ReturnType
Code
export class Test {
myMethod(): string {
return "42";
}
myMethod2(a: string): string {
return "42" + a;
}
// Works ---
override<M extends keyof this>(method: M, newFunc: this[M]) {
}
// Works ---
override2<M extends keyof this>(method: M, newFunc: (...args: Parameters<this["myMethod2"]>) => ReturnType<this["myMethod2"]>) {
}
// Fails ---
override3<M extends keyof this>(method: M, newFunc: (...args: Parameters<this[M]>) => ReturnType<this[M]>) {
}
}
const test = new Test();
test.override("myMethod2", a => "xxx");
test.override2("myMethod2", a => "xxx");
test.override3("myMethod2", a => "xxx");
Expected behavior:
I would expect override3 to compile and be equivalent to override + override2 (when M == "myMethod2")
Actual behavior:
override3 causes syntax error:
Type 'this[M]' does not satisfy the constraint '(...args: any) => any'.
Type 'this[keyof this]' is not assignable to type '(...args: any) => any'.
Type 'this[string] | this[number] | this[symbol]' is not assignable to type '(...args: any) => any'.
Type 'this[string]' is not assignable to type '(...args: any) => any'.
Related Issues:
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 bản tái hiện được liên kết trong TypeScript Playground và kiểm tra cách trình biên dịch xử lý Parameters và ReturnType khi T là generic được lập chỉ mục this[M]. Issue được hoàn tất khi ví dụ override3 biên dịch với kết quả suy luận như mong đợi cho myMethod2 và hành vi này được bao phủ bởi một bài kiểm thử trình biên dịch phù hợp.
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
- Lỗi
- Độ 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