Argument of type 'Date' is not assignable to parameter of type 'string'

Habib picture Habib · Dec 28, 2016 · Viewed 53.1k times · Source

I am working on Angular2-Meteor project and I am getting this

Argument of type 'Date' is not assignable to parameter of type 'string'.

error when I wrote this function to set value in date.

start: startOfDay(new Date(attendance.dayAt)),

I have tried to set the type as Date but won't work how can I resolve this error?

Answer

Egon Allison picture Egon Allison · Jun 23, 2017

I had a similar problem.

After researching I found three useful links :

Date variable works, but functions on it do not

Converting string to date issue

Invalid argument 'date format' for pipe 'DatePipe'?

What I then did was to declare my variable as type Date in the "export class" component like this :

start: Date

and then later on in a function I used it like this to populate the date variable:

start = new Date(Date.now());

Judging on the error you are receiving I assume that your function "startOfDay()" is returning a string so you would then have to change your code as follows.

start: new Date(startOfDay(new Date(attendance.dayAt)))

reason for this being that if "start" is a date then you have to use the function new Date() to assign a date to it. Date then takes string as input and returns a date and assigns it to variable "start". Hope this helps :)