AgedPeanuts
AgedPeanuts

Reputation: 145

Property 'toDate' does not exist on type 'FieldValue | Timestamp' but WORKS

I'm using Cloud Firestore to store Chats, which I would like to sort by lastestMessage.created. I was not able to use the query because I was using it for something else so I'm using the SORT function to do it.

I keep getting this error on VS Code:

Property 'toDate' does not exist on type 'FieldValue | Timestamp'.

However, the code runs just fine, it compares it and returns it correctly. With no errors in the console.

      chats.sort(function (a, b) {
        if (b.latestMessage.created && a.latestMessage.created) {
          return b.latestMessage.created.toDate() - a.latestMessage.created.toDate();
        }
      });


(parameter) a: {
    contractId?: string;
    members: any;
    contact: any;
    latestMessage?: Message;
    id?: string;
    created?: firebase.firestore.FieldValue | firebase.firestore.Timestamp;
    modified?: firebase.firestore.FieldValue | firebase.firestore.Timestamp;
}

It's just annoying that this false ERROR keeps showing when I compile and that it works perfectly...

Upvotes: 0

Views: 1820

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

Your created and modified properties are both typed to accept either s FieldValue or Timestamp type object. It's not clear to me what that's necessary. Since the compiler doesn't know which one it is, you can only call methods that they have in common, so toDate is not value.

If you want it to be a Timestamp type that you can call toDate, then you will have to either remove FieldValue from the possible type, or cast the value to Timestamp, so the compiler knows exactly what you think you're dealing with.

Upvotes: 2

Related Questions