将插件方法添加到 Flow 中的 Moment.js 声明?
Add plugin method to Moment.js declaration in Flow?
有没有办法 extend/add 使用 Flow 到现有声明?
具体来说,在本例中我们同时使用 Moment.js, and a plugin that adds a format method to the Duration object。
我在 FlowInterfaces 找到了一个 third-party type declaration for momentjs,但是(当然)它没有插件提供的额外方法。
目前为了解决这个问题,我复制了整个声明文件,并为新方法添加了一行,但理想情况下,有一种方法可以更新现有的声明,例如:
type moment$MomentOptions += {
format(format: ?string): string; // moment-duration-format
}
...但语法更好 :)
所以我的问题是 Flow 是否存在这样的东西?
遗憾的是,如果不覆盖整个定义,这是不可能的 currently。
目前,您还可以扩展 moment$Moment
并在您的项目中改用此类型。但它需要投射大部分结果。因此,不确定这是否是更好的解决方案;但在某些情况下可能会有所帮助。
declare class moment$MyMoment extends moment$Moment {
format(format: ?string): string;
}
function fn(m: moment$MyMoment) {
m.format(''); // OK
}
const m = ((moment(new Date()): any): moment$MyMoment);
fn(m); // OK
有没有办法 extend/add 使用 Flow 到现有声明?
具体来说,在本例中我们同时使用 Moment.js, and a plugin that adds a format method to the Duration object。
我在 FlowInterfaces 找到了一个 third-party type declaration for momentjs,但是(当然)它没有插件提供的额外方法。
目前为了解决这个问题,我复制了整个声明文件,并为新方法添加了一行,但理想情况下,有一种方法可以更新现有的声明,例如:
type moment$MomentOptions += {
format(format: ?string): string; // moment-duration-format
}
...但语法更好 :)
所以我的问题是 Flow 是否存在这样的东西?
遗憾的是,如果不覆盖整个定义,这是不可能的 currently。
目前,您还可以扩展 moment$Moment
并在您的项目中改用此类型。但它需要投射大部分结果。因此,不确定这是否是更好的解决方案;但在某些情况下可能会有所帮助。
declare class moment$MyMoment extends moment$Moment {
format(format: ?string): string;
}
function fn(m: moment$MyMoment) {
m.format(''); // OK
}
const m = ((moment(new Date()): any): moment$MyMoment);
fn(m); // OK