概要
mixinというか多重継承みたいになったが以下でできた。
export type Constructor<T = {}> = new (...args: any[]) => T;
class EmptyClass {}
export function SomeMixin<TBase extends Constructor>(Base: TBase) {
return class extends Base {
private privateValue = 1;
protected protectedValue = 1;
publicValue = 1;
someMethod() {
console.log(this.privateValue);
}
};
}
@Injectable({ providedIn: 'root' })
export class SomeService extends SomeMixin(EmptyClass) {
// ここに普通に実装できる
constructor(private router: Router) {}
}
動機
- typescriptにはmixinが用意されていないが単一継承だと足りない場合もいつかあるのでどうにかしたかった。
調査
-
公式のやつはプロパティ初期化が無視される
https://www.typescriptlang.org/docs/handbook/mixins.html -
typescript用と銘打たれてる以下のやつはそのまま使うとexport対象がクラスじゃないのでDIで弾かれる
https://typescript-jp.gitbook.io/deep-dive/type-system/mixins- これをextendsに使ったらうまく行った。
あとがき
- 結果的に継承してるだけになったので既に他の子クラスになってるやつ以外は普通に継承を使ったほうが読みやすいと思う。
- もっと良い書き方あればください。