【发布时间】:2020-03-20 09:20:53
【问题描述】:
我发现自己重复了很多代码,大致如下:
interface IMyInterface {
commonA: string;
commonB: string;
}
class Foo implements IMyInterface {
commonA: string = "hello";
commonB: string = "world";
foo: string = "Foo!";
}
class Bar implements IMyInterface {
commonA: string = "hello";
commonB: string = "world";
bar: string = "Bar!";
}
在我的例子中,我有很多类都共享commonA 和commonB,但在其他方面有所不同。现在,我知道我不能在界面中设置默认值,但是是否有一些设计模式或 TS 构造可以让我在一个地方定义默认值?
【问题讨论】:
标签: typescript class design-patterns interface