@@ -41,14 +41,11 @@ import { FirestoreDataConverter } from './snapshot';
41
41
import { NestedUpdateFields , Primitive } from './types' ;
42
42
43
43
/**
44
- * Document data (for use with {@link @firebase/firestore/lite#(setDoc:1) }) consists of fields mapped to
45
- * values .
44
+ * Document data (for use with {@link @firebase/firestore/lite#(setDoc:1) }).
45
+ * Must be an object with string keys .
46
46
*/
47
- export interface DocumentData {
48
- /** A mapping between a field and its value. */
49
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
- [ field : string ] : any ;
51
- }
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ export type DocumentData = Record < string , any > ;
52
49
53
50
/**
54
51
* Similar to Typescript's `Partial<T>`, but allows nested fields to be
@@ -113,7 +110,7 @@ export type SetOptions =
113
110
* and can be used to write, read, or listen to the location. The document at
114
111
* the referenced location may or may not exist.
115
112
*/
116
- export class DocumentReference < T = DocumentData > {
113
+ export class DocumentReference < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > {
117
114
/** The type of this Firestore reference. */
118
115
readonly type = 'document' ;
119
116
@@ -129,7 +126,7 @@ export class DocumentReference<T = DocumentData> {
129
126
/**
130
127
* If provided, the `FirestoreDataConverter` associated with this instance.
131
128
*/
132
- readonly converter : FirestoreDataConverter < T > | null ,
129
+ readonly converter : FirestoreDataConverter < AppType , DbType > | null ,
133
130
readonly _key : DocumentKey
134
131
) {
135
132
this . firestore = firestore ;
@@ -157,8 +154,8 @@ export class DocumentReference<T = DocumentData> {
157
154
/**
158
155
* The collection this `DocumentReference` belongs to.
159
156
*/
160
- get parent ( ) : CollectionReference < T > {
161
- return new CollectionReference < T > (
157
+ get parent ( ) : CollectionReference < AppType , DbType > {
158
+ return new CollectionReference < AppType , DbType > (
162
159
this . firestore ,
163
160
this . converter ,
164
161
this . _key . path . popLast ( )
@@ -175,26 +172,26 @@ export class DocumentReference<T = DocumentData> {
175
172
* @param converter - Converts objects to and from Firestore.
176
173
* @returns A `DocumentReference<U>` that uses the provided converter.
177
174
*/
178
- withConverter < U > ( converter : FirestoreDataConverter < U > ) : DocumentReference < U > ;
175
+ withConverter < NewAppType = DocumentData , NewDbType extends DocumentData = NewAppType extends DocumentData ? NewAppType : DocumentData > ( converter : FirestoreDataConverter < NewAppType , NewDbType > ) : DocumentReference < NewAppType , NewDbType > ;
179
176
/**
180
177
* Removes the current converter.
181
178
*
182
179
* @param converter - `null` removes the current converter.
183
180
* @returns A `DocumentReference<DocumentData>` that does not use a converter.
184
181
*/
185
182
withConverter ( converter : null ) : DocumentReference < DocumentData > ;
186
- withConverter < U > (
187
- converter : FirestoreDataConverter < U > | null
188
- ) : DocumentReference < U > {
189
- return new DocumentReference < U > ( this . firestore , converter , this . _key ) ;
183
+ withConverter < NewAppType = DocumentData , NewDbType extends DocumentData = NewAppType extends DocumentData ? NewAppType : DocumentData > (
184
+ converter : FirestoreDataConverter < NewAppType , NewDbType > | null
185
+ ) : DocumentReference < NewAppType , NewDbType > {
186
+ return new DocumentReference < NewAppType , NewDbType > ( this . firestore , converter , this . _key ) ;
190
187
}
191
188
}
192
189
193
190
/**
194
191
* A `Query` refers to a query which you can read or listen to. You can also
195
192
* construct refined `Query` objects by adding filters and ordering.
196
193
*/
197
- export class Query < T = DocumentData > {
194
+ export class Query < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > {
198
195
/** The type of this Firestore reference. */
199
196
readonly type : 'query' | 'collection' = 'query' ;
200
197
@@ -212,7 +209,7 @@ export class Query<T = DocumentData> {
212
209
/**
213
210
* If provided, the `FirestoreDataConverter` associated with this instance.
214
211
*/
215
- readonly converter : FirestoreDataConverter < T > | null ,
212
+ readonly converter : FirestoreDataConverter < AppType , DbType > | null ,
216
213
readonly _query : InternalQuery
217
214
) {
218
215
this . firestore = firestore ;
@@ -224,7 +221,7 @@ export class Query<T = DocumentData> {
224
221
* @param converter - `null` removes the current converter.
225
222
* @returns A `Query<DocumentData>` that does not use a converter.
226
223
*/
227
- withConverter ( converter : null ) : Query < DocumentData > ;
224
+ withConverter ( converter : null ) : Query < DocumentData , DocumentData > ;
228
225
/**
229
226
* Applies a custom data converter to this query, allowing you to use your own
230
227
* custom model objects with Firestore. When you call {@link getDocs} with
@@ -234,24 +231,24 @@ export class Query<T = DocumentData> {
234
231
* @param converter - Converts objects to and from Firestore.
235
232
* @returns A `Query<U>` that uses the provided converter.
236
233
*/
237
- withConverter < U > ( converter : FirestoreDataConverter < U > ) : Query < U > ;
238
- withConverter < U > ( converter : FirestoreDataConverter < U > | null ) : Query < U > {
239
- return new Query < U > ( this . firestore , converter , this . _query ) ;
234
+ withConverter < NewAppType = DocumentData , NewDbType extends DocumentData = NewAppType extends DocumentData ? NewAppType : DocumentData > ( converter : FirestoreDataConverter < NewAppType , NewDbType > ) : Query < NewAppType , NewDbType > ;
235
+ withConverter < NewAppType = DocumentData , NewDbType extends DocumentData = NewAppType extends DocumentData ? NewAppType : DocumentData > ( converter : FirestoreDataConverter < NewAppType , NewDbType > | null ) : Query < NewAppType , NewDbType > {
236
+ return new Query < NewAppType , NewDbType > ( this . firestore , converter , this . _query ) ;
240
237
}
241
238
}
242
239
243
240
/**
244
241
* A `CollectionReference` object can be used for adding documents, getting
245
242
* document references, and querying for documents (using {@link (query:1)}).
246
243
*/
247
- export class CollectionReference < T = DocumentData > extends Query < T > {
244
+ export class CollectionReference < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > extends Query < AppType , DbType > {
248
245
/** The type of this Firestore reference. */
249
246
readonly type = 'collection' ;
250
247
251
248
/** @hideconstructor */
252
249
constructor (
253
250
firestore : Firestore ,
254
- converter : FirestoreDataConverter < T > | null ,
251
+ converter : FirestoreDataConverter < AppType , DbType > | null ,
255
252
readonly _path : ResourcePath
256
253
) {
257
254
super ( firestore , converter , newQueryForPath ( _path ) ) ;
@@ -274,12 +271,12 @@ export class CollectionReference<T = DocumentData> extends Query<T> {
274
271
* A reference to the containing `DocumentReference` if this is a
275
272
* subcollection. If this isn't a subcollection, the reference is null.
276
273
*/
277
- get parent ( ) : DocumentReference < DocumentData > | null {
274
+ get parent ( ) : DocumentReference < DocumentData , DocumentData > | null {
278
275
const parentPath = this . _path . popLast ( ) ;
279
276
if ( parentPath . isEmpty ( ) ) {
280
277
return null ;
281
278
} else {
282
- return new DocumentReference (
279
+ return new DocumentReference < DocumentData , DocumentData > (
283
280
this . firestore ,
284
281
/* converter= */ null ,
285
282
new DocumentKey ( parentPath )
@@ -296,21 +293,19 @@ export class CollectionReference<T = DocumentData> extends Query<T> {
296
293
* @param converter - Converts objects to and from Firestore.
297
294
* @returns A `CollectionReference<U>` that uses the provided converter.
298
295
*/
299
- withConverter < U > (
300
- converter : FirestoreDataConverter < U >
301
- ) : CollectionReference < U > ;
296
+ withConverter < NewAppType = DocumentData , NewDbType extends DocumentData = NewAppType extends DocumentData ? NewAppType : DocumentData > (
297
+ converter : FirestoreDataConverter < NewAppType , NewDbType >
298
+ ) : CollectionReference < NewAppType , NewDbType > ;
302
299
/**
303
300
* Removes the current converter.
304
301
*
305
302
* @param converter - `null` removes the current converter.
306
303
* @returns A `CollectionReference<DocumentData>` that does not use a
307
304
* converter.
308
305
*/
309
- withConverter ( converter : null ) : CollectionReference < DocumentData > ;
310
- withConverter < U > (
311
- converter : FirestoreDataConverter < U > | null
312
- ) : CollectionReference < U > {
313
- return new CollectionReference < U > ( this . firestore , converter , this . _path ) ;
306
+ withConverter ( converter : null ) : CollectionReference < DocumentData , DocumentData > ;
307
+ withConverter < NewAppType = DocumentData , NewDbType extends DocumentData = NewAppType extends DocumentData ? NewAppType : DocumentData > ( converter : FirestoreDataConverter < NewAppType , NewDbType > | null ) : CollectionReference < NewAppType , NewDbType > {
308
+ return new CollectionReference < NewAppType , NewDbType > ( this . firestore , converter , this . _path ) ;
314
309
}
315
310
}
316
311
@@ -330,7 +325,7 @@ export function collection(
330
325
firestore : Firestore ,
331
326
path : string ,
332
327
...pathSegments : string [ ]
333
- ) : CollectionReference < DocumentData > ;
328
+ ) : CollectionReference < DocumentData , DocumentData > ;
334
329
/**
335
330
* Gets a `CollectionReference` instance that refers to a subcollection of
336
331
* `reference` at the the specified relative path.
@@ -343,11 +338,11 @@ export function collection(
343
338
* to a collection.
344
339
* @returns The `CollectionReference` instance.
345
340
*/
346
- export function collection (
347
- reference : CollectionReference < unknown > ,
341
+ export function collection < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > (
342
+ reference : CollectionReference < AppType , DbType > ,
348
343
path : string ,
349
344
...pathSegments : string [ ]
350
- ) : CollectionReference < DocumentData > ;
345
+ ) : CollectionReference < DocumentData , DocumentData > ;
351
346
/**
352
347
* Gets a `CollectionReference` instance that refers to a subcollection of
353
348
* `reference` at the the specified relative path.
@@ -360,16 +355,16 @@ export function collection(
360
355
* to a collection.
361
356
* @returns The `CollectionReference` instance.
362
357
*/
363
- export function collection (
364
- reference : DocumentReference ,
358
+ export function collection < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > (
359
+ reference : DocumentReference < AppType , DbType > ,
365
360
path : string ,
366
361
...pathSegments : string [ ]
367
- ) : CollectionReference < DocumentData > ;
368
- export function collection (
369
- parent : Firestore | DocumentReference < unknown > | CollectionReference < unknown > ,
362
+ ) : CollectionReference < DocumentData , DocumentData > ;
363
+ export function collection < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > (
364
+ parent : Firestore | DocumentReference < AppType , DbType > | CollectionReference < AppType , DbType > ,
370
365
path : string ,
371
366
...pathSegments : string [ ]
372
- ) : CollectionReference < DocumentData > {
367
+ ) : CollectionReference < DocumentData , DocumentData > {
373
368
parent = getModularInstance ( parent ) ;
374
369
375
370
validateNonEmptyArgument ( 'collection' , 'path' , path ) ;
@@ -392,7 +387,7 @@ export function collection(
392
387
ResourcePath . fromString ( path , ...pathSegments )
393
388
) ;
394
389
validateCollectionPath ( absolutePath ) ;
395
- return new CollectionReference (
390
+ return new CollectionReference < DocumentData , DocumentData > (
396
391
parent . firestore ,
397
392
/* converter= */ null ,
398
393
absolutePath
@@ -417,7 +412,7 @@ export function collection(
417
412
export function collectionGroup (
418
413
firestore : Firestore ,
419
414
collectionId : string
420
- ) : Query < DocumentData > {
415
+ ) : Query < DocumentData , DocumentData > {
421
416
firestore = cast ( firestore , Firestore ) ;
422
417
423
418
validateNonEmptyArgument ( 'collectionGroup' , 'collection id' , collectionId ) ;
@@ -429,7 +424,7 @@ export function collectionGroup(
429
424
) ;
430
425
}
431
426
432
- return new Query (
427
+ return new Query < DocumentData , DocumentData > (
433
428
firestore ,
434
429
/* converter= */ null ,
435
430
newQueryForCollectionGroup ( collectionId )
@@ -452,7 +447,7 @@ export function doc(
452
447
firestore : Firestore ,
453
448
path : string ,
454
449
...pathSegments : string [ ]
455
- ) : DocumentReference < DocumentData > ;
450
+ ) : DocumentReference < DocumentData , DocumentData > ;
456
451
/**
457
452
* Gets a `DocumentReference` instance that refers to a document within
458
453
* `reference` at the specified relative path. If no path is specified, an
@@ -468,11 +463,11 @@ export function doc(
468
463
* a document.
469
464
* @returns The `DocumentReference` instance.
470
465
*/
471
- export function doc < T > (
472
- reference : CollectionReference < T > ,
466
+ export function doc < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > (
467
+ reference : CollectionReference < AppType , DbType > ,
473
468
path ?: string ,
474
469
...pathSegments : string [ ]
475
- ) : DocumentReference < T > ;
470
+ ) : DocumentReference < AppType , DbType > ;
476
471
/**
477
472
* Gets a `DocumentReference` instance that refers to a document within
478
473
* `reference` at the specified relative path.
@@ -485,16 +480,16 @@ export function doc<T>(
485
480
* a document.
486
481
* @returns The `DocumentReference` instance.
487
482
*/
488
- export function doc (
489
- reference : DocumentReference < unknown > ,
483
+ export function doc < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > (
484
+ reference : DocumentReference < AppType , DbType > ,
490
485
path : string ,
491
486
...pathSegments : string [ ]
492
- ) : DocumentReference < DocumentData > ;
493
- export function doc < T > (
494
- parent : Firestore | CollectionReference < T > | DocumentReference < unknown > ,
487
+ ) : DocumentReference < DocumentData , DocumentData > ;
488
+ export function doc < AppType = DocumentData , DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData > (
489
+ parent : Firestore | CollectionReference < AppType , DbType > | DocumentReference < AppType , DbType > ,
495
490
path ?: string ,
496
491
...pathSegments : string [ ]
497
- ) : DocumentReference {
492
+ ) : DocumentReference < AppType , DbType > {
498
493
parent = getModularInstance ( parent ) ;
499
494
500
495
// We allow omission of 'pathString' but explicitly prohibit passing in both
@@ -543,9 +538,9 @@ export function doc<T>(
543
538
* @returns true if the references point to the same location in the same
544
539
* Firestore database.
545
540
*/
546
- export function refEqual < T > (
547
- left : DocumentReference < T > | CollectionReference < T > ,
548
- right : DocumentReference < T > | CollectionReference < T >
541
+ export function refEqual < AppType , DbType extends DocumentData > (
542
+ left : DocumentReference < AppType , DbType > | CollectionReference < AppType , DbType > ,
543
+ right : DocumentReference < AppType , DbType > | CollectionReference < AppType , DbType >
549
544
) : boolean {
550
545
left = getModularInstance ( left ) ;
551
546
right = getModularInstance ( right ) ;
@@ -573,7 +568,7 @@ export function refEqual<T>(
573
568
* @returns true if the references point to the same location in the same
574
569
* Firestore database.
575
570
*/
576
- export function queryEqual < T > ( left : Query < T > , right : Query < T > ) : boolean {
571
+ export function queryEqual < AppType , DbType extends DocumentData > ( left : Query < AppType , DbType > , right : Query < AppType , DbType > ) : boolean {
577
572
left = getModularInstance ( left ) ;
578
573
right = getModularInstance ( right ) ;
579
574
0 commit comments