microsoft / microsoft/TypeScript

Mixins created from `obj.constructor` cannot have members

未关闭
#61,255 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Domain: classes Help Wanted Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 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_

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行链接的 TypeScript Playground 复现,并比较 Mixin1 与 Mixin2,重点关注使用 obj.constructor 的类继承表达式。当 Mixin2 能够保留其 foof 成员且不再出现所报告的继承错误,同时 Mixin1 仍能编译时,该 Issue 即完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。