22

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?

eko
  • 39,722
  • 10
  • 72
  • 98
Habib
  • 846
  • 2
  • 9
  • 24
  • 2
    Give a little more detail please. What is `startOfDay` function? – eko Dec 28 '16 at 05:23
  • I am using RxJs map and javascript map to insert data in Angular2 Calendar, so it also provides events the "startOfDay" is the field in calendar events. so my inserting function looks like this ` return { start: startOfDay(new Date(attendance.dayAt)), title: attendance.status, color: this.getColor(attendance.status) } ` so how can I define the type of Date in startOfDay in typescript? – Habib Dec 28 '16 at 06:26
  • 1
    What is Angular2 Calender? A library? – eko Dec 28 '16 at 06:27
  • yes Angular2-Calendar is a module – Habib Dec 28 '16 at 06:27
  • Any links to its documentation? – eko Dec 28 '16 at 06:27
  • 2
    Btw can you try `start: startOfDay(new Date(attendance.dayAt).toString())` to see if it works? – eko Dec 28 '16 at 06:29
  • yes I tried but its not working and the link for "Angular2-Calendar" are : [link](https://www.npmjs.com/package/angular2-calendar) [link](https://mattlewis92.github.io/angular-bootstrap-calendar/#?example=kitchen-sink) – Habib Dec 28 '16 at 06:33
  • I don't see any `start` config, I see `startsAt` – eko Dec 28 '16 at 06:37
  • but tell me how can I define of that? I mean to say that its working fine and everything is working fine, but the only problem is I am getting warning which I describe in the Head of this Question. – Habib Dec 28 '16 at 06:46
  • What I understand from your question is that either `start:` or `startOfDay()` (you even don't specify this) expects a string but you assign a Date. So the answer must be either `startOfDay(new Date(attendance.dayAt).toString())` or `startOfDay(new Date(attendance.dayAt)).toString()` – eko Dec 28 '16 at 06:54
  • I tried it, but its not working. – Habib Dec 28 '16 at 07:11
  • @habib M.Farooq i think you are facing typescript error. new Date(attendance.dayAt) is of type data and startOfDay() is expecting a string. so can you try this startOfDay(new Date(attendance.dayAt)+'') you have already tried using . toString() and if its possible change startOfDay() argument type to any – Amit kumar Dec 28 '16 at 07:20
  • I tried it and it also not working... – Habib Dec 28 '16 at 09:30
  • so what you will advice me to do? Please kindly resolve this issue... – Habib Dec 29 '16 at 04:58

3 Answers3

24

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 :)

Egon Allison
  • 1,329
  • 1
  • 13
  • 22
3

I have a situation where a date value is being returned from a web service as a JSON string and a date is being cast into a Date type property in typeScript. This causes 'Type 'string' is not assignable to type 'Date'' error to be shown during the compilation.

One work-around I've found is to define client side property as 'any' type (instead of :Date). This allows JSON to nicely cast into a date and no error is shown during the compilation.

0

if you are retrieving a Date as a string from an API you need to convert your angular date field from it:

getUser(id: number): Observable<User> {
    return this.http.get<User>(this.baseUrl + 'users/' + id.toString())
    .pipe(
      map( user => {
        user.lastActive = new Date(user.lastActive);
        return user;
      })
    );
  }