microsoft / microsoft/TypeScript

TS2345 when calling a superclass' method with a conditional type parameter from a parameterized subclass

Đang mở
#38,359 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Domain: Conditional Types
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.8.3 & 4.0.0-dev.20200506

Search Terms: TS2345 generic constructor super superclass method type parameter inheritance conditional type constraint

Code

type FilterKeysOf<T, Condition> = { [K in keyof T]: T[K] extends Condition ? K : never }[keyof T];

type ArrayKeysOf<T> = FilterKeysOf<T, readonly unknown[]>;

interface I {
  foo: string;
  bar: string[];
  baz: number;
}

interface J extends I {
  qux: boolean[];
}

class A<T extends I> {
  protected treasure: T

  constructor(treasure: T) {
    this.treasure = treasure;
  }

  getArrayTreasure<K extends ArrayKeysOf<T>>(fieldName: K): T[K] {
    return this.treasure[fieldName];
  }
}

class B<T extends J> extends A<T> {
  getQuxArrayTreasure(): boolean[] {
    return super.getArrayTreasure('qux');
  }
}

class C extends B<J> {
  anyQux(): boolean {
    return super.getArrayTreasure('qux').some(Boolean);
  }
}

Expected behavior:
Compiles without errors.

Actual behavior:
TS v3.8.3:

foo.ts:25:35 - error TS2345: Argument of type '"qux"' is not assignable to parameter of type 'T[keyof T] extends readonly unknown[] ? keyof T : never'.

25     return super.getArrayTreasure('qux');
                                     ~~~~~


Found 1 error.

TS v4.0.0-dev.20200506:

foo.ts:25:5 - error TS2322: Type 'T[{ [K in keyof T]: T[K] extends readonly unknown[] ? K : never; }[keyof T]]' is not assignable to type 'boolean[]'.
  Type 'T[T[keyof T] extends readonly unknown[] ? keyof T : never]' is not assignable to type 'boolean[]'.
    Type 'T[keyof T]' is not assignable to type 'boolean[]'.
      Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'boolean[]'.
        Type 'T[string]' is not assignable to type 'boolean[]'.

25     return super.getArrayTreasure('qux');

foo.ts:25:35 - error TS2345: Argument of type '"qux"' is not assignable to parameter of type 'T[keyof T] extends readonly unknown[] ? keyof T : never'.

25     return super.getArrayTreasure('qux');
                                     ~~~~~


Found 2 errors.

Playground Link:
Link
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

Tái hiện ví dụ kế thừa generic được cung cấp trong TypeScript Playground được liên kết bằng các phiên bản TypeScript được liệt kê. Vì không có tệp repository hoặc test nào được nêu, hãy xác định luồng kiểm tra kiểu của compiler dành cho các kiểu điều kiện và các lệnh gọi phương thức của lớp cha, sau đó thêm một regression test cho thấy ví dụ biên dịch thành công mà không có các chẩn đoán được báo cáo.

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ó
4/5
Thời gian dự kiến
3-5 ngày
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

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.