microsoft / microsoft/TypeScript

Mixins created from `obj.constructor` cannot have members

オープン
#61,255 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Domain: classes Help Wanted Possible Improvement
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、リンク先の TypeScript Playground の再現コードを実行し、Mixin1 と Mixin2 を比較します。obj.constructor を使用しているクラスの継承式に注目してください。Mixin2 が報告された継承エラーなしで foof メンバーを保持でき、Mixin1 は引き続きコンパイルできれば、Issue は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。