File tree Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 1
- import firestore , { firebase } from '../lib' ;
1
+ import firestore , { firebase , FirebaseFirestoreTypes } from '../lib' ;
2
2
3
3
const COLLECTION = 'firestore' ;
4
4
@@ -306,5 +306,27 @@ describe('Storage', function () {
306
306
} ,
307
307
} ) ;
308
308
} ) ;
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
+ } ) ;
309
331
} ) ;
310
332
} ) ;
Original file line number Diff line number Diff line change @@ -429,7 +429,7 @@ export namespace FirebaseFirestoreTypes {
429
429
* @param data A map of the fields and values for the document.
430
430
* @param options An object to configure the set behavior.
431
431
*/
432
- set ( data : T , options ?: SetOptions ) : Promise < void > ;
432
+ set ( data : SetValue < T > , options ?: SetOptions ) : Promise < void > ;
433
433
434
434
/**
435
435
* Updates fields in the document referred to by this `DocumentReference`. The update will fail
@@ -448,7 +448,7 @@ export namespace FirebaseFirestoreTypes {
448
448
*
449
449
* @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.
450
450
*/
451
- update ( data : Partial < { [ K in keyof T ] : T [ K ] | FieldValue } > ) : Promise < void > ;
451
+ update ( data : Partial < SetValue < T > > ) : Promise < void > ;
452
452
453
453
/**
454
454
* Updates fields in the document referred to by this DocumentReference. The update will fail if
@@ -2080,6 +2080,17 @@ export namespace FirebaseFirestoreTypes {
2080
2080
*/
2081
2081
useEmulator ( host : string , port : number ) : void ;
2082
2082
}
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 ;
2083
2094
}
2084
2095
2085
2096
declare const defaultExport : ReactNativeFirebase . FirebaseModuleWithStaticsAndApp <
You can’t perform that action at this time.
0 commit comments