microsoft / microsoft/TypeScript

Mixins created from `obj.constructor` cannot have members

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

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

Domain: classes Help Wanted Possible Improvement
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ả

### 🔎 Search Terms

mixin constructor property instance

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about mixins

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqAfFALxQIQDuAFAHQMCGqA5kgFxSMIgDaAugEoyJAgG4AUBMwAbRkiRQAYnDhQA3hKhQA9DqgB1XAGsuqOOgQATKAAtgwMBz0sAlsFvoARnUxwAtjr+rpjmSHAAZsA6BOAQAMqhrmDRrgroEEg6AMwAHAAsAIxaUFYQsszQvABEfshoWDio1fyc8PUY2Lh4HmlEkgC+UhGW2K6IUACyrgAergiFhFAQM8AQ1ortKJ1NeCpwREQ0SOiQqBUKnATCmtp6UADyANIlqBDA6KgIUBeKK2sbKAnM6-DQlbQRVQRGg3IbaAaDYajYDjb7TOYIABMS3+6ysin2RzgXgAVlcbiV7jA5AooAByGgAQQQiBA-gsil+gjpUHmflQb2wMhAy1WeMUXnk0FBdIIdLolP09KZLJ47PQnJpSG5vMU8iQrhYCEYXhk0BwUA80sQ20Y82AUEilri9LlABooF50A7ZTy-OgZDYvNB5ihuCjGGsbFR3LYuKVXBEIhA3ggHScvKBII6Ij8bWg7Wn6ft5a93p9vqDcYDiSTfPmdrgwdoIVCYRo4VAERIhkA

### 💻 Code

```ts
type Constructor = new(...args: any[]) => T;

class Foo {
// Work around https://github.com/microsoft/TypeScript/issues/3841
declare ["constructor"]: Constructor;
}

function Mixin1>(superclass: T) {
// OK
return class extends superclass {
foof() {}
};
}

function Mixin2(obj: T) {
// Class '(Anonymous class)' incorrectly extends base class 'T'.
// '(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Foo'.
return class extends obj.constructor {
foof() {}
};
}
```

### 🙁 Actual behavior

`Mixin1` compiles fine, but `Mixin2` has an error:

```
Class '(Anonymous class)' incorrectly extends base class 'T'.
'(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Foo'.
```

Removing the `foof` member causes it to compile without error.

### 🙂 Expected behavior

Both should compile fine because they are doing the same thing.

### 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 bằng cách chạy bản tái hiện được liên kết trong TypeScript Playground và so sánh Mixin1 với Mixin2, tập trung vào biểu thức kế thừa lớp sử dụng obj.constructor. Issue được hoàn tất khi Mixin2 có thể giữ lại thành viên foof mà không gặp lỗi kế thừa đã báo cáo, trong khi Mixin1 vẫn tiếp tục biên dịch.

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