microsoft / microsoft/TypeScript
Mixin / Inheritance problem with Typescript - string not assignable to string|number|symbol
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Bug Report
I have a typescript class. I'd like to subclass it and add a mixin. However, it keeps telling me that string not assignable to string|number|symbol. I don't understand why because the method signatures are the same.
🔎 Search Terms
typescript mixin prototype
🕗 Version & Regression Information
4.9.5
I just started using Typescript, so 4.9.5 is the version I was using since I started seeing it.
⏯ Playground Link
this shows the problem with UserRecord
this shows the way to work around the problem
💻 Code
This is as short as I can make the example and still show what's happening:
type TModel = {
'User' : {
username : string,
password : string | undefined,
},
'APIKey' : {
privateKey: string,
}
};
type TProps<T extends keyof TModel> = Partial<TModel[T]>;
type Constructor<T = {}> = new (...args: any[]) => T;
class VingRecord<T extends keyof TModel> {
constructor( private props: TProps<T>) { }
public set<K extends keyof TProps<T>>(_key: K) {}
}
function RoleMixin<T extends Constructor>(Base: T) {
return class RoleMixin extends Base { };
}
class UserRecord extends RoleMixin(VingRecord<'User'>) {
public set<K extends keyof TProps<'User'>>(_key: K) {}
}
and if you replace the RoleMixin with this it hides the problem with the funky prototype:
function RoleMixin<T extends IConstructable>(Base: T) {
class RoleMixin extends Base {};
return RoleMixin as {
new (...args: any): RoleMixin;
prototype: any;
} & T;
}
🙁 Actual behavior
The code works, but typescript has a problem with the mixin thinking the type on VingRecord is any rather than 'User'. So it reports a type mismatch on the UserRecord.
More detail here with the guys who spotted the behavior
🙂 Expected behavior
I should be able to do the inheritance as shown in the code sample above without having to do the funky return from the mixin.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground の再現例から始め、リンクされた回避策と比較しながら、ジェネリックな mixin と継承の処理に焦点を当てます。元の例が回避策なしで型チェックに通り、UserRecord を通じて 'User' 型が保持されれば、issue は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100