I'm using TypeScript to define the data models for my documents in Firebase. When the document is being created the createdOn field will be the server timestamp sentinal, but otherwise it is a date. For example...
export interface Post<MODE extends 'create' | 'read'> {
comment: string;
createdOn: MODE extends 'create' ? FieldValue : Date;
}
I don't know how to properly define and assign to the createdOn property. First, I'm not really sure which firebase module to load the FieldValue type from. The best I could do was this and this seems a bit hokey...
import * as firebase from 'firebase';
type FieldValue = firebase.default.firestore.FieldValue;
export const SERVER_TIMESTAMP = firebase.default.firestore.FieldValue.serverTimestamp();
And then when I use it, this is what happens...
// Fails when used within cloud function
// Detected an object of type "FieldValue" that doesn't match the
// expected instance (found in field "createdOn"). Please ensure
/// that the Firestore types you are using are from the same NPM package.
post.createdOn = SERVER_TIMESTAMP;
// But this works within cloud function
post.createdOn = admin.firestore.FieldValue.serverTimestamp();
Are the types of FieldValue different when called from within a cloud function and on the client? Can I simply import this type from some module that works from both cloud functions and client?