microsoft / microsoft/TypeScript

Mixin class with type param seems to lose information about default type parameter, causing "base constructors must all have the same return type"

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

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

Bug Domain: classes Help Wanted
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

🔎 Search Terms

Mixin class with type param seems to lose information about default type parameter, causing "base constructors must all have the same return type"

Related:
https://github.com/microsoft/TypeScript/issues/37142
https://github.com/microsoft/TypeScript/issues/40110

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about mixins and type parameters
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/CYUwxgNghgTiAEAXAngBwQYQPYDsDOiMArmIljACpogA8FAfPALzxQBGBMUp8OIA7vAAUASmaMKAbgBQ0sLgLwAQrHYQQAMWbw6KvAhAAPRCBzA88bPkIkylajQDybAFbhE9ekL0gAXPAofMSZGAG9peEio9k5uRHhIKDwLFS42dRpU7U4ASxwAc0YjEzMUpIRwqKqotlghQ39cgrFQ+DhEIhgceENJeABfCOrB6raQDq7lVXSQGX6ZOWhkqeB4YtNzKbT1DSFnN1IWwcWkiwBxLCxV9dKttU0913cRGibC+FD+oA

💻 Code
declare type ConstructorType<T> = abstract new () => T;

const BarableF = <TBase extends ConstructorType<Object>>(Base: TBase) => {
      abstract class Barable<Bar = string> extends Base {
          bar(x: string) { return x; }
      }
      return Barable;
};

class Bad extends BarableF(Object) {}

class Good extends BarableF(Object)<string> {}
🙁 Actual behavior

The line class Bad extends BarableF(Object) {} fails because "Base constructors must all have the same return type". If I hover over BarableF, I see the type

const BarableF: <ObjectConstructor>(Base: ObjectConstructor) => ((abstract new <**Bar**>() => BarableF<ObjectConstructor>.Barable<Bar>) & {
    ...;
}) & ObjectConstructor

where notably Bar has lost its default type param, which might be causing the issue.

🙂 Expected behavior

I would expect Bad to type-check without a problem, because in the Good example below it, you see that providing a type argument that is the same as the default for the type param causes things to work.

Additional information about the issue

No response

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 TypeScript Playground được liên kết và rút gọn các ví dụ Bad và Good thành một trường hợp hồi quy. Điều tra việc suy luận constructor của generic mixin và lý do tham số kiểu mặc định của Bar bị mất; hoàn thành khi Bad được kiểm tra kiểu mà không cần đối số string tường minh, còn Good vẫn tiếp tục hoạt động. Thêm trường hợp này vào khu vực kiểm thử compiler tương ứng sau khi đã xác định được vị trí của hành vi.

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
38/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.