2016-06-14 76 views
0

有沒有辦法使用Flow擴展/添加到現有的聲明?將插件方法添加到Flow中的Moment.js聲明中?

具體而言,在這種情況下,我們同時使用了Moment.jsa 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 
} 

。 ..但更好的語法:)

所以我的問題是這樣的事情存在的流?

回答

1

不幸的是,沒有壓倒整個定義,這是不可能的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