microsoft / microsoft/TypeScript

Parameters<T> and ReturnType<T> limitation

Đang mở
#38,685 2 bình luận 9 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ả

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

Playground Link:
https://www.typescriptlang.org/play/?ssl=28&ssc=14&pln=28&pc=15#code/MYGwhgzhAEAqCmEAu0DeAod1vQLYE8BZeJACwHsATACgEoAuaZAJwEsA7AczSxz+ZIBXZu2gAiACwAmMQG5e2AL6Y+BYmSpTqYRiw6cGTJGy48+-ISPHSx0ANTQw8vsoXQA9O+wB1cswDWMNAAtKFu5ABu8MxslPAAPITQ8AAeSPDslDD+8PjkAGbQZKwQAHzUuCQUlIyEADTQ7PAA7gBiguzAjMUQANqEALq0ZjiufJ4+foHYocHhUTGscVKJyWkZWdA5eYU95ZUaNdD1jS3tnYzUAHQ3YMycEIwACndgB9EQ8T29YmpVmmIBqVhgBeUrQABKlnYsHwAAcEt9fkR-pQZEDhhgXCocBNoK0wKwQEFZvNorF4ABmVapdKZbK5ApFUglfao2oNJptDpdaDXW73R7QF7MN4kD5fFl9QbA6BgyHQ2EIyUlfoYkZKdCuYDkdjIIqIFAg07NOCGujydDpZBXSLkpbwajI9TVGQNMBy8FiFI+sS0eTWpC2hYUrTO1Fuxye8Q+lJ+gOG4P2uKUp1-Q6Rj3y72+-3oIA

Related Issues:

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

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

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.