Javascript 如何在不复制类型规范的情况下对类及其默认值重复使用flowtype类型定义?
我正在尝试做的示例:Javascript 如何在不复制类型规范的情况下对类及其默认值重复使用flowtype类型定义?,javascript,flowtype,Javascript,Flowtype,我正在尝试做的示例: //@flow interface MyLibDefaults { foo: string, bar: boolean }; class MyLib implements MyLibDefaults { defaults: MyLibDefaults; // do I really have to put these type specifications again? There are a lot of them.... foo: strin
//@flow
interface MyLibDefaults {
foo: string,
bar: boolean
};
class MyLib implements MyLibDefaults {
defaults: MyLibDefaults;
// do I really have to put these type specifications again? There are a lot of them....
foo: string;
bar: false;
constructor() {
this.defaults = {
foo: "",
bar: false
};
Object.assign(this, defaults);
}
///...
resetToDefaults() {
Object.assign(this, defaults);
}
}
也许接口不是正确的方法,但我不知道如何让我的类“使用自己的默认值扩展
”类型定义 来自Flow的文档:
Flow还支持使用(…)当使用此语法时,不需要为其提供类型注释。但如果你需要,你仍然可以
因此,您可以:
//@flow
interface MyLibDefaults {
foo: string,
bar: boolean
};
class MyLib implements MyLibDefaults {
defaults: MyLibDefaults;
foo = "";
bar = false;
constructor() {
this.defaults = {
foo: this.foo,
bar: this.bar,
};
}
///...
resetToDefaults() {
Object.assign(this, this.defaults);
}
}
当然,你需要