diff --git a/packages/firestore/__tests__/firestore.test.ts b/packages/firestore/__tests__/firestore.test.ts index 79aabda18b..d0ccee469b 100644 --- a/packages/firestore/__tests__/firestore.test.ts +++ b/packages/firestore/__tests__/firestore.test.ts @@ -1,4 +1,4 @@ -import firestore, { firebase } from '../lib'; +import firestore, { firebase, FirebaseFirestoreTypes } from '../lib'; const COLLECTION = 'firestore'; @@ -306,5 +306,27 @@ describe('Storage', function () { }, }); }); + + it('does not throw when Date is provided instead of Timestamp', async function () { + type BarType = { + myDate: FirebaseFirestoreTypes.Timestamp; + }; + + const docRef = firebase.firestore().doc(`${COLLECTION}/bar`); + await docRef.set({ + myDate: new Date(), + }); + }); + + it('does not throw when serverTimestamp is provided instead of Timestamp', async function () { + type BarType = { + myDate: FirebaseFirestoreTypes.Timestamp; + }; + + const docRef = firebase.firestore().doc(`${COLLECTION}/bar`); + await docRef.set({ + myDate: firestore.FieldValue.serverTimestamp(), + }); + }); }); }); diff --git a/packages/firestore/lib/index.d.ts b/packages/firestore/lib/index.d.ts index d248f9816c..ae7f97bba4 100644 --- a/packages/firestore/lib/index.d.ts +++ b/packages/firestore/lib/index.d.ts @@ -429,7 +429,7 @@ export namespace FirebaseFirestoreTypes { * @param data A map of the fields and values for the document. * @param options An object to configure the set behavior. */ - set(data: T, options?: SetOptions): Promise; + set(data: SetValue, options?: SetOptions): Promise; /** * Updates fields in the document referred to by this `DocumentReference`. The update will fail @@ -448,7 +448,7 @@ export namespace FirebaseFirestoreTypes { * * @param data An object containing the fields and values with which to update the document. Fields can contain dots to reference nested fields within the document. */ - update(data: Partial<{ [K in keyof T]: T[K] | FieldValue }>): Promise; + update(data: Partial>): Promise; /** * Updates fields in the document referred to by this DocumentReference. The update will fail if @@ -2080,6 +2080,17 @@ export namespace FirebaseFirestoreTypes { */ useEmulator(host: string, port: number): void; } + + /** + * Utility type to allow FieldValue and to allow Date in place of Timestamp objects. + */ + export type SetValue = T extends Timestamp + ? Timestamp | Date // allow Date in place of Timestamp + : T extends object + ? { + [P in keyof T]: SetValue | FieldValue; // allow FieldValue in place of values + } + : T; } declare const defaultExport: ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<