Skip to content

Commit 5f4eadf

Browse files
authored
fix(firestore, types): allow FieldValues, Date and Timestamp in doc set and update (#5901)
* Allow setting FieldValues and allow Date in place of Timestamp * Add tests
1 parent f7313b9 commit 5f4eadf

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

packages/firestore/__tests__/firestore.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import firestore, { firebase } from '../lib';
1+
import firestore, { firebase, FirebaseFirestoreTypes } from '../lib';
22

33
const COLLECTION = 'firestore';
44

@@ -306,5 +306,27 @@ describe('Storage', function () {
306306
},
307307
});
308308
});
309+
310+
it('does not throw when Date is provided instead of Timestamp', async function () {
311+
type BarType = {
312+
myDate: FirebaseFirestoreTypes.Timestamp;
313+
};
314+
315+
const docRef = firebase.firestore().doc<BarType>(`${COLLECTION}/bar`);
316+
await docRef.set({
317+
myDate: new Date(),
318+
});
319+
});
320+
321+
it('does not throw when serverTimestamp is provided instead of Timestamp', async function () {
322+
type BarType = {
323+
myDate: FirebaseFirestoreTypes.Timestamp;
324+
};
325+
326+
const docRef = firebase.firestore().doc<BarType>(`${COLLECTION}/bar`);
327+
await docRef.set({
328+
myDate: firestore.FieldValue.serverTimestamp(),
329+
});
330+
});
309331
});
310332
});

packages/firestore/lib/index.d.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ export namespace FirebaseFirestoreTypes {
429429
* @param data A map of the fields and values for the document.
430430
* @param options An object to configure the set behavior.
431431
*/
432-
set(data: T, options?: SetOptions): Promise<void>;
432+
set(data: SetValue<T>, options?: SetOptions): Promise<void>;
433433

434434
/**
435435
* Updates fields in the document referred to by this `DocumentReference`. The update will fail
@@ -448,7 +448,7 @@ export namespace FirebaseFirestoreTypes {
448448
*
449449
* @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.
450450
*/
451-
update(data: Partial<{ [K in keyof T]: T[K] | FieldValue }>): Promise<void>;
451+
update(data: Partial<SetValue<T>>): Promise<void>;
452452

453453
/**
454454
* Updates fields in the document referred to by this DocumentReference. The update will fail if
@@ -2080,6 +2080,17 @@ export namespace FirebaseFirestoreTypes {
20802080
*/
20812081
useEmulator(host: string, port: number): void;
20822082
}
2083+
2084+
/**
2085+
* Utility type to allow FieldValue and to allow Date in place of Timestamp objects.
2086+
*/
2087+
export type SetValue<T> = T extends Timestamp
2088+
? Timestamp | Date // allow Date in place of Timestamp
2089+
: T extends object
2090+
? {
2091+
[P in keyof T]: SetValue<T[P]> | FieldValue; // allow FieldValue in place of values
2092+
}
2093+
: T;
20832094
}
20842095

20852096
declare const defaultExport: ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<

0 commit comments

Comments
 (0)