简体   繁体   English

带有 Typescript 的 Momentjs:“类型字符串不可分配给类型 'Date'”

[英]Momentjs with Typescript : “type string is not assignable to type 'Date'”

Till now I was using momentjs library like this:到目前为止,我一直在使用这样的 momentjs 库:

import * as moment from 'moment'; // import moment.
@Component({..});
export class TestClass {
  lastUpdated = Date
  constructor(private myService: MyService){
    this.lastUpdated = this.getCurrentTime(); **// this shows error.. that type string is not assignable to type 'Date'**
  }
 var getCurrentTime(){
    return moment().format('DD MMM YYYY HH:mm:ss'); 
 }
}

Above code shows me this error: that type string is not assignable to type 'Date'**上面的代码向我显示了这个错误:该类型字符串不可分配给类型 'Date'**

But same code exactly working fine when I use this line in place of the import module, evenn other resturent os但是当我使用这一行代替导入模块时,相同的代码完全正常工作,甚至其他餐厅操作系统

declare var moment: any;声明 var 时刻:任何;

But I canot use above statement as it is using "var": and 'any' is not suggested by Lint.但是我不能使用上面的语句,因为它使用的是“var”:并且 Lint 不建议使用“any”。

Why getCurrentTime is returning String?为什么 getCurrentTime 返回 String? Should not this return Date应该不是这个返回日期

“type string is not assignable to type 'Date'” “类型字符串不能分配给类型'日期'”

lastUpdated = Date should be lastUpdated: string . lastUpdated = Date应该是lastUpdated: string

Code代码

Fixed a few other things as well:还修复了其他一些问题:

import * as moment from 'moment'; // import moment.
@Component({..});
export class TestClass {
  lastUpdated: string;
  constructor(private myService: MyService){
    this.lastUpdated = this.getCurrentTime();
  }
 getCurrentTime(){
    return moment().format('DD MMM YYYY HH:mm:ss'); 
 }
}

Can avoid calling one more function by the below code可以避免通过下面的代码再调用一个函数


import * as moment from 'moment'; // import moment.
@Component({..});
export class TestClass {
  lastUpdated: string;
  constructor(private myService: MyService){
    this.lastUpdated = moment(this.lastUpdated).format('DD MMM YYYY HH:mm:ss');
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿类型&#39;数字&#39;不可分配│键入&#39;字符串&#39; - Typescript Type 'number' is not assignable │ to type 'string' &#39;{&#39;Authorization&#39;:string; }”不可分配给“ IHttpRequestConfigHeaders”类型 - '{ 'Authorization': string; }' is not assignable to type 'IHttpRequestConfigHeaders' type script 类型参数 &#39;{ type: string[]; }&#39; 不可分配给“BlobPropertyBag”类型的参数 - type script Argument of type '{ type: string[]; }' is not assignable to parameter of type 'BlobPropertyBag' Typescript 1.8到2.5错误类型IPromise &lt;{}&gt;&#39;不能分配给类型&#39;IPromise <IWebErrors[]> - Typescript 1.8 to 2.5 error Type IPromise<{}>' is not assignable to type 'IPromise<IWebErrors[]> 获取momentjs中两个日期字符串之间的差异? - Get Diffrence between two date string in momentjs? 类型“可观察” <any> &#39;不可分配给&#39;Observable类型 <T> &#39; - Type 'Observable<any>' is not assignable to type 'Observable<T>' 类型&#39;()=&gt; JQuery&#39;的参数不能分配给类型&#39;()=&gt; boolean&#39;的参数 - Argument of type '() => JQuery' is not assignable to parameter of type '() => boolean' 类型“ PersonAddress []”不可分配给类型“可观察” <PersonAddress[]> &#39;角度2 - Type 'PersonAddress[]' is not assignable to type 'Observable<PersonAddress[]>' in angular 2 角度绑定字符串日期到输入类型日期 - Angular bind string date to input type date angular2的TypeScript错误:字符串不可分配 - TypeScript Error with angular2 : string is not assignable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM