简体   繁体   中英

error TS2322: Type '() => string' is not assignable to type 'string'

I am new to Angular 6. With the following code:

export class DateComponent implements OnInit {

  currentDate: string = new Date().toDateString;

  constructor() { }

  ngOnInit() {
  }
}

I am getting the following error, but I do not know what may be causing it.

error TS2322: Type '() => string' is not assignable to type 'string'.

You should invoke the function, not assign the function object. Adding () at the end should fix it

currentDate: string = new Date().toDateString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM