我目前正在将我的项目从ES5转换为ES6,但是在使用MomentJS(版本2.18.1
)时遇到了问题。问题是我有一些变量是Moment对象,但我无法对它们调用moment()。
例如:
import * as moment from "moment";
export class DateThingy {
constructor(private moment) { //What type does this have??
}
public getDate(): moment.Moment {
return this.moment();
}
}
1) 如果我将 private moment
的类型设置为 private moment: moment
,WebStorm 将会提示:"cannot find name 'moment'."。
2) 如果我将类型设置为 private moment: moment.Moment
,该对象已经更改,我不能再调用 this.moment()
了(它现在是一个对象,不再有函数调用)。Webstorm 告诉我:"cannot invoke an expression whose type lacks a call signature. Type 'Moment' has no compatible call signatures."。
3) 我不能再使用 MomentStatic,因为它没有被导出。如果我键入 private moment: moment.MomentStatic
,WebStorm 将会报告:"namespace 'moment' does not have an exported member 'MomentStatic'"。
因此,在这个例子中,我应该使用什么样的输入?
moment
可以使用,例如getDate(): moment.Moment { return moment(); }
。 - Heretic Monkeymoment.d.ts
。顺便说一下,moment
是一个命名空间,而不是一个类型。 - user663031moment
也是一个函数(declare function moment(): moment.Moment
)。但我想知道如何为变量指定类型,以便可以在其上调用该函数?(moment
在其接口上有一个名为()
的函数,但我似乎无法为变量指定moment
类型) - Mr.wiseguymoment.Moment
。 - user663031moment()
或()
. - Mr.wiseguy