在 TypeScript 中使用 MomentJS - moment() 有什么类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45171252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using MomentJS with TypeScript - What type does moment() have?
提问by Mr.wiseguy
I am currently converting my project from ES5 to ES6, but I am running into an issue with MomentJS (version 2.18.1
). The problem is that I have a few variables that are Moment objects, but I cannot call moment() on them.
我目前正在将我的项目从 ES5 转换为 ES6,但我遇到了 MomentJS ( version 2.18.1
) 问题。问题是我有一些变量是 Moment 对象,但我不能对它们调用 moment() 。
An example:
一个例子:
import * as moment from "moment";
export class DateThingy {
constructor(private moment) { //What type does this have??
}
public getDate(): moment.Moment {
return this.moment();
}
}
1) If I set the type of private moment
to private moment: moment
WebStorm complains: "cannot find name 'moment'."
1) 如果我将类型设置private moment
为private moment: moment
WebStorm 抱怨:“找不到名称‘时刻’。”
2) If I set the type to private moment: moment.Moment
the object has changed and I cannot call this.moment()
anymore (it is now an object and does not have a function call on it). Webstorm tells me: "cannot invoke an expression whose type lacks a call signiture. Type 'Moment' has no campatible call signatures."
2)如果我将类型设置private moment: moment.Moment
为对象已经改变并且我不能再调用this.moment()
(它现在是一个对象并且没有函数调用)。Webstorm 告诉我:“无法调用类型缺少调用签名的表达式。类型 'Moment' 没有可兼容的调用签名。”
3) I cannot use MomentStatic anymore, since it is not exported. If I type private moment: moment.MomentStatic
WebStorm gives me: "namespace 'moment' does not have an exported member 'MomentStatic'"
3) 我不能再使用 MomentStatic,因为它没有被导出。如果我输入private moment: moment.MomentStatic
WebStorm 给我:“命名空间‘moment’没有导出成员‘MomentStatic’”
So what typing should I use for this example?
那么我应该在这个例子中使用什么类型?
回答by Mr.wiseguy
As Mike McCaughansaid, the moment object cannot be injected in the constructor. Somehow this was possible with an old version of MomentJS. this could be resolved by removing the constructor property and accessing the global moment object that is included via import * as moment from "moment"
.
正如Mike McCaughan所说,moment 对象不能被注入到构造函数中。不知何故,旧版本的 MomentJS 可以做到这一点。这可以通过删除构造函数属性并访问通过import * as moment from "moment"
.
The function moment()
returns a Moment
object. This can be typed via moment.Moment
.
该函数moment()
返回一个Moment
对象。这可以通过moment.Moment
.
So the code can be rewritten as follows:
所以代码可以改写如下:
import * as moment from "moment";
export class DateThingy{
constructor() {
}
public getDate(): moment.Moment {
return moment();
}
}
回答by OHM
Did you tried importing moment without alias?
您是否尝试过导入没有别名的时刻?
import moment from 'moment';
import moment from 'moment';
This worked for me. And typescript compiler won't complain about it.
这对我有用。而且打字稿编译器不会抱怨它。
const date = moment().format('YYYYMMDD');
const date = moment().format('YYYYMMDD');
Note that this requires a tsconfig update!
请注意,这需要 tsconfig 更新!
In the TSConfig you need to add the option allowSyntheticDefaultImportsto allow default imports of libraries that have no default exports.
在 TSConfig 中,您需要添加选项allowSyntheticDefaultImports以允许默认导入没有默认导出的库。
Example (tsconfig.json):
示例(tsconfig.json):
{
"compileOnSave": false,
"compilerOptions": {
"allowSyntheticDefaultImports": true,
}
}
回答by shashi kumar
I got Error message like this
我收到了这样的错误消息
Cannot invoke an expression whose type lacks a call signature. Type 'typeof moment' has no compatible call signatures.
无法调用类型缺少调用签名的表达式。类型 'typeof moment' 没有兼容的调用签名。
My issue was importing with alias
我的问题是使用别名导入
import * as momentns from 'moment';
从“时刻”导入 * 作为时刻;
I changed this to
我把这个改成
import moment from 'moment';
从“时刻”导入时刻;
solved for me in angular 8(TypeScript 2.4)
为我解决了 angular 8(TypeScript 2.4)