継承が必要ない場合は他の方が書いた下記記事を参考にして下さい。
https://qiita.com/nwtgck/items/bbfd6e3ca16857eb9c34
ts-copyable:上で紹介されていたnpmパッケージ
applyMixins関数:ここからutils.ts
にコピペしておいて下さい。export忘れずに。
mixinを使う。
import Copyable, { PartialMap } from "ts-copyable";
import { applyMixins } from "./utils";
export abstract class Base<C extends Base<C>>{
abstract readonly a: number;
abstract readonly b: string;
abstract copy(partial: Partial<Base<C>>): C;
abstract mapCopy(partial: PartialMap<Base<C>>): C;
addA(x: number): C {
return this.mapCopy({ a: a => a + x })
}
setB(b: string): C {
return this.copy({ b });
}
}
export class Child extends Copyable<Child> implements Base<Child> {
constructor(
public readonly a: number,
public readonly b: string,
public readonly c: boolean) {
super(Child);
}
addA: (x: number) => Child;
setB: (b: string) => Child;
addA2(x: number): Child {
return this.addA(x).addA(x);
}
setC(c: boolean): Child {
return this.copy({ c });
}
}
applyMixins(Child, [Base]);
Scala使いたい!