Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeScript error on Firebase Cloud Function: Property 'toJSON' is missing in type 'FirebaseFirestore.Timestamp'

I am writing a Firebase Cloud Function, and I’m trying to convert a datetime string to a Firebase Timestamp.

I have been following advice from this and this answer, but TypeScript is complaining with this error:

Property ‘toJSON’ is missing in type ‘FirebaseFirestore.Timestamp’ but
required in type
‘import("/Users/John/Dev/my-app/node_modules/@firebase/firestore/dist/index").Timestamp’.
ts(2741)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is what my code looks like:

import * as admin from 'firebase-admin';
import { Timestamp } from 'firebase/firestore';
interface FirestoreSerp {
  createdAt: Timestamp;
  keyword: string;
}
const datetime = '2019-11-15 12:57:46 +00:00'
const firestoreSerp: FirestoreSerp = {
  createdAt: admin.firestore.Timestamp.fromDate(new Date(datetime)), // <-- ERROR HERE
  keyword: 'rickety cricket',
};

>Solution :

The client SDK (firebase/firestore) uses slightly different type definitions to the admin SDK (firebase-admin) and that is what is causing your conflict.

Use the same package to extract the types.

import * as admin from 'firebase-admin';

interface FirestoreSerp {
  createdAt: admin.firebase.Timestamp;
  keyword: string;
}

const datetime = '2019-11-15 12:57:46 +00:00'
const firestoreSerp: FirestoreSerp = {
  createdAt: admin.firestore.Timestamp.fromDate(new Date(datetime)),
  keyword: 'rickety cricket',
};

To alias the type:

type Timestamp = admin.firestore.Timestamp;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading