@@ -9,7 +9,12 @@ import { FirebaseApp } from '@firebase/app-exp';
9
9
import { LogLevelString as LogLevel } from ' @firebase/logger' ;
10
10
11
11
// @public
12
- export function addDoc<T >(reference : CollectionReference <T >, data : T ): Promise <DocumentReference <T >>;
12
+ export function addDoc<T >(reference : CollectionReference <T >, data : WithFieldValue <T >): Promise <DocumentReference <T >>;
13
+
14
+ // @public
15
+ export type AddPrefixToKeys <Prefix extends string , T extends Record <string , unknown >> = {
16
+ [K in keyof T & string as ` ${Prefix }.${K } ` ]+ ? : T [K ];
17
+ };
13
18
14
19
// @public
15
20
export function arrayRemove(... elements : unknown []): FieldValue ;
@@ -134,8 +139,8 @@ export class Firestore {
134
139
// @public
135
140
export interface FirestoreDataConverter <T > {
136
141
fromFirestore(snapshot : QueryDocumentSnapshot <DocumentData >): T ;
137
- toFirestore(modelObject : T ): DocumentData ;
138
- toFirestore(modelObject : Partial <T >, options : SetOptions ): DocumentData ;
142
+ toFirestore(modelObject : WithFieldValue < T > ): DocumentData ;
143
+ toFirestore(modelObject : PartialWithFieldValue <T >, options : SetOptions ): DocumentData ;
139
144
}
140
145
141
146
// @public
@@ -184,12 +189,25 @@ export function limitToLast(limit: number): QueryConstraint;
184
189
185
190
export { LogLevel }
186
191
192
+ // @public
193
+ export type NestedUpdateFields <T extends Record <string , unknown >> = UnionToIntersection <{
194
+ [K in keyof T & string ]: T [K ] extends Record <string , unknown > ? AddPrefixToKeys <K , UpdateData <T [K ]>> : never ;
195
+ }[keyof T & string ]>;
196
+
187
197
// @public
188
198
export function orderBy(fieldPath : string | FieldPath , directionStr ? : OrderByDirection ): QueryConstraint ;
189
199
190
200
// @public
191
201
export type OrderByDirection = ' desc' | ' asc' ;
192
202
203
+ // @public
204
+ export type PartialWithFieldValue <T > = T extends Primitive ? T : T extends {} ? {
205
+ [K in keyof T ]? : PartialWithFieldValue <T [K ]> | FieldValue ;
206
+ } : Partial <T >;
207
+
208
+ // @public
209
+ export type Primitive = string | number | boolean | undefined | null ;
210
+
193
211
// @public
194
212
export class Query <T = DocumentData > {
195
213
protected constructor ();
@@ -239,10 +257,10 @@ export function runTransaction<T>(firestore: Firestore, updateFunction: (transac
239
257
export function serverTimestamp(): FieldValue ;
240
258
241
259
// @public
242
- export function setDoc<T >(reference : DocumentReference <T >, data : T ): Promise <void >;
260
+ export function setDoc<T >(reference : DocumentReference <T >, data : WithFieldValue < T > ): Promise <void >;
243
261
244
262
// @public
245
- export function setDoc<T >(reference : DocumentReference <T >, data : Partial <T >, options : SetOptions ): Promise <void >;
263
+ export function setDoc<T >(reference : DocumentReference <T >, data : PartialWithFieldValue <T >, options : SetOptions ): Promise <void >;
246
264
247
265
// @public
248
266
export function setLogLevel(logLevel : LogLevel ): void ;
@@ -304,19 +322,22 @@ export class Timestamp {
304
322
export class Transaction {
305
323
delete(documentRef : DocumentReference <unknown >): this ;
306
324
get<T >(documentRef : DocumentReference <T >): Promise <DocumentSnapshot <T >>;
307
- set<T >(documentRef : DocumentReference <T >, data : T ): this ;
308
- set<T >(documentRef : DocumentReference <T >, data : Partial <T >, options : SetOptions ): this ;
309
- update(documentRef : DocumentReference <unknown >, data : UpdateData ): this ;
325
+ set<T >(documentRef : DocumentReference <T >, data : WithFieldValue < T > ): this ;
326
+ set<T >(documentRef : DocumentReference <T >, data : PartialWithFieldValue <T >, options : SetOptions ): this ;
327
+ update< T > (documentRef : DocumentReference <T >, data : UpdateData < T > ): this ;
310
328
update(documentRef : DocumentReference <unknown >, field : string | FieldPath , value : unknown , ... moreFieldsAndValues : unknown []): this ;
311
329
}
312
330
313
331
// @public
314
- export interface UpdateData {
315
- [fieldPath : string ]: any ;
316
- }
332
+ export type UnionToIntersection <U > = (U extends unknown ? (k : U ) => void : never ) extends (k : infer I ) => void ? I : never ;
317
333
318
334
// @public
319
- export function updateDoc(reference : DocumentReference <unknown >, data : UpdateData ): Promise <void >;
335
+ export type UpdateData <T > = T extends Primitive ? T : T extends Map <infer K , infer V > ? Map <UpdateData <K >, UpdateData <V >> : T extends {} ? {
336
+ [K in keyof T ]? : UpdateData <T [K ]> | FieldValue ;
337
+ } & NestedUpdateFields <T > : Partial <T >;
338
+
339
+ // @public
340
+ export function updateDoc<T >(reference : DocumentReference <T >, data : UpdateData <T >): Promise <void >;
320
341
321
342
// @public
322
343
export function updateDoc(reference : DocumentReference <unknown >, field : string | FieldPath , value : unknown , ... moreFieldsAndValues : unknown []): Promise <void >;
@@ -327,13 +348,18 @@ export function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value
327
348
// @public
328
349
export type WhereFilterOp = ' <' | ' <=' | ' ==' | ' !=' | ' >=' | ' >' | ' array-contains' | ' in' | ' array-contains-any' | ' not-in' ;
329
350
351
+ // @public
352
+ export type WithFieldValue <T > = T extends Primitive ? T : T extends {} ? {
353
+ [K in keyof T ]: WithFieldValue <T [K ]> | FieldValue ;
354
+ } : Partial <T >;
355
+
330
356
// @public
331
357
export class WriteBatch {
332
358
commit(): Promise <void >;
333
359
delete(documentRef : DocumentReference <unknown >): WriteBatch ;
334
- set<T >(documentRef : DocumentReference <T >, data : T ): WriteBatch ;
335
- set<T >(documentRef : DocumentReference <T >, data : Partial <T >, options : SetOptions ): WriteBatch ;
336
- update(documentRef : DocumentReference <unknown >, data : UpdateData ): WriteBatch ;
360
+ set<T >(documentRef : DocumentReference <T >, data : WithFieldValue < T > ): WriteBatch ;
361
+ set<T >(documentRef : DocumentReference <T >, data : PartialWithFieldValue <T >, options : SetOptions ): WriteBatch ;
362
+ update< T > (documentRef : DocumentReference <T >, data : UpdateData < T > ): WriteBatch ;
337
363
update(documentRef : DocumentReference <unknown >, field : string | FieldPath , value : unknown , ... moreFieldsAndValues : unknown []): WriteBatch ;
338
364
}
339
365
0 commit comments