How to check if an object is a firestore.Timestamp?

Viewed 1236

I'm working on a validation function.

Is there a way to check if an object is a firestore.Timestamp ?

I'm using the firebase JS SDK and also using Typescript.

import * as firebase from "firebase/app";

const blogPost = {
  title: "",
  createdAt: firebase.firestore.Timestamp.now()
}

function validateBlogPostCreationDate(blogPost) {
  // SOMETHING LIKE THIS:
  return blogPost.createdAt is a Timestamp ?
    true : false;
}
1 Answers

JavaScript and TypeScript have an instanceof operator that checks the type of an object against a prototype.

return blogPost.createdAt instanceof firebase.firestore.Timestamp
Related