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)
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',
};
CodePudding user response:
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;