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