Skip to content

Commit aecc8fa

Browse files
author
Brian Chen
committed
incremental fixes
1 parent fdd432a commit aecc8fa

12 files changed

+123
-81
lines changed

packages/firestore/src/api/database.ts

+13-23
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ import {
127127
NextFn,
128128
PartialObserver
129129
} from './observer';
130-
import { NestedPartialWithFieldValue, WithFieldValue } from '../lite/reference';
130+
import { NestedPartial, WithFieldValue } from '../lite/reference';
131131

132132
/**
133133
* A persistence provider for either memory-only or IndexedDB persistence.
@@ -441,22 +441,19 @@ export class Transaction implements PublicTransaction, Compat<ExpTransaction> {
441441

442442
set<T>(
443443
documentRef: DocumentReference<T>,
444-
data: NestedPartialWithFieldValue<T>,
444+
data: Partial<T>,
445445
options: PublicSetOptions
446446
): Transaction;
447-
set<T>(
448-
documentRef: DocumentReference<T>,
449-
data: WithFieldValue<T>
450-
): Transaction;
447+
set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
451448
set<T>(
452449
documentRef: PublicDocumentReference<T>,
453-
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
450+
data: T | Partial<T>,
454451
options?: PublicSetOptions
455452
): Transaction {
456453
const ref = castReference(documentRef);
457454
if (options) {
458455
validateSetOptions('Transaction.set', options);
459-
this._delegate.set(ref, data as NestedPartialWithFieldValue<T>, options);
456+
this._delegate.set(ref, data as NestedPartial<T>, options);
460457
} else {
461458
this._delegate.set(ref, data as WithFieldValue<T>);
462459
}
@@ -505,22 +502,19 @@ export class WriteBatch implements PublicWriteBatch, Compat<ExpWriteBatch> {
505502
constructor(readonly _delegate: ExpWriteBatch) {}
506503
set<T>(
507504
documentRef: DocumentReference<T>,
508-
data: NestedPartialWithFieldValue<T>,
505+
data: Partial<T>,
509506
options: PublicSetOptions
510507
): WriteBatch;
511-
set<T>(
512-
documentRef: DocumentReference<T>,
513-
data: WithFieldValue<T>
514-
): WriteBatch;
508+
set<T>(documentRef: DocumentReference<T>, data: Partial<T>): WriteBatch;
515509
set<T>(
516510
documentRef: PublicDocumentReference<T>,
517-
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
511+
data: T | Partial<T>,
518512
options?: PublicSetOptions
519513
): WriteBatch {
520514
const ref = castReference(documentRef);
521515
if (options) {
522516
validateSetOptions('WriteBatch.set', options);
523-
this._delegate.set(ref, data as NestedPartialWithFieldValue<T>, options);
517+
this._delegate.set(ref, data as NestedPartial<T>, options);
524518
} else {
525519
this._delegate.set(ref, data as WithFieldValue<T>);
526520
}
@@ -606,11 +600,11 @@ class FirestoreDataConverter<U>
606600

607601
toFirestore(modelObject: WithFieldValue<U>): PublicDocumentData;
608602
toFirestore(
609-
modelObject: NestedPartialWithFieldValue<U>,
603+
modelObject: NestedPartial<U>,
610604
options: PublicSetOptions
611605
): PublicDocumentData;
612606
toFirestore(
613-
modelObject: WithFieldValue<U> | NestedPartialWithFieldValue<U>,
607+
modelObject: WithFieldValue<U> | NestedPartial<U>,
614608
options?: PublicSetOptions
615609
): PublicDocumentData {
616610
if (!options) {
@@ -741,11 +735,7 @@ export class DocumentReference<T = PublicDocumentData>
741735
options = validateSetOptions('DocumentReference.set', options);
742736
try {
743737
if (options) {
744-
return setDoc(
745-
this._delegate,
746-
value as NestedPartialWithFieldValue<T>,
747-
options
748-
);
738+
return setDoc(this._delegate, value as NestedPartial<T>, options);
749739
} else {
750740
return setDoc(this._delegate, value as WithFieldValue<T>);
751741
}
@@ -1302,7 +1292,7 @@ export class CollectionReference<T = PublicDocumentData>
13021292
}
13031293

13041294
add(data: T): Promise<DocumentReference<T>> {
1305-
return addDoc(this._delegate, data).then(
1295+
return addDoc(this._delegate, data as WithFieldValue<T>).then(
13061296
docRef => new DocumentReference(this.firestore, docRef)
13071297
);
13081298
}

packages/firestore/src/exp/reference_impl.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
CollectionReference,
4343
doc,
4444
DocumentReference,
45-
NestedPartialWithFieldValue,
45+
NestedPartial,
4646
Query,
4747
SetOptions,
4848
TypedUpdateData,
@@ -261,20 +261,20 @@ export function setDoc<T>(
261261
*/
262262
export function setDoc<T>(
263263
reference: DocumentReference<T>,
264-
data: NestedPartialWithFieldValue<T>,
264+
data: NestedPartial<T>,
265265
options: SetOptions
266266
): Promise<void>;
267267
export function setDoc<T>(
268268
reference: DocumentReference<T>,
269-
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
269+
data: WithFieldValue<T> | NestedPartial<T>,
270270
options?: SetOptions
271271
): Promise<void> {
272272
reference = cast<DocumentReference<T>>(reference, DocumentReference);
273273
const firestore = cast(reference.firestore, Firestore);
274274

275275
const convertedValue = applyFirestoreDataConverter(
276276
reference.converter,
277-
data,
277+
data as WithFieldValue<T>,
278278
options
279279
);
280280
const dataReader = newUserDataReader(firestore);

packages/firestore/src/exp/snapshot.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { ChangeType, ViewSnapshot } from '../core/view_snapshot';
2020
import { FieldPath } from '../lite/field_path';
2121
import {
2222
DocumentData,
23-
NestedPartialWithFieldValue,
23+
NestedPartial,
2424
Query,
2525
queryEqual,
2626
SetOptions,
@@ -99,10 +99,7 @@ export interface FirestoreDataConverter<T>
9999
* Firestore database). Used with {@link (setDoc:1)}, {@link (WriteBatch.set:1)}
100100
* and {@link (Transaction.set:1)} with `merge:true` or `mergeFields`.
101101
*/
102-
toFirestore(
103-
modelObject: NestedPartialWithFieldValue<T>,
104-
options: SetOptions
105-
): DocumentData;
102+
toFirestore(modelObject: NestedPartial<T>, options: SetOptions): DocumentData;
106103

107104
/**
108105
* Called by the Firestore SDK to convert Firestore data into an object of

packages/firestore/src/lite/reference.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ export interface DocumentData {
5050
}
5151

5252
type Primitive = string | number | boolean | bigint | undefined | null;
53-
// eslint-disable-next-line @typescript-eslint/ban-types
54-
type Builtin = Primitive | Function;
5553

5654
/** Like Partial but recursive */
57-
export type NestedPartialWithFieldValue<T> = T extends Builtin
55+
export type NestedPartial<T> = T extends Primitive
5856
? T
5957
: T extends Map<infer K, infer V>
60-
? Map<NestedPartialWithFieldValue<K>, NestedPartialWithFieldValue<V>>
58+
? Map<NestedPartial<K>, NestedPartial<V>>
6159
: T extends {}
62-
? { [K in keyof T]?: NestedPartialWithFieldValue<T[K]> | FieldValue }
60+
? { [K in keyof T]?: NestedPartial<T[K]> | FieldValue }
6361
: Partial<T>;
6462

65-
export type WithFieldValue<T> = { [P in keyof T]: T[P] | FieldValue };
63+
export type WithFieldValue<T> = T extends Primitive
64+
? T
65+
: T extends {}
66+
? { [K in keyof T]: WithFieldValue<T[K]> | FieldValue }
67+
: Partial<T>;
6668

6769
/**
6870
* Update data (for use with {@link @firebase/firestore/lite#(updateDoc:1)}) consists of field paths (e.g.
@@ -76,7 +78,7 @@ export interface UpdateData {
7678
}
7779
// Represents an update object to Firestore document data, which can contain either fields like {a: 2}
7880
// or dot-separated paths such as {"a.b" : 2} (which updates the nested property "b" in map field "a").
79-
export type TypedUpdateData<T> = T extends Builtin
81+
export type TypedUpdateData<T> = T extends Primitive
8082
? T
8183
: T extends Map<infer K, infer V>
8284
? Map<TypedUpdateData<K>, TypedUpdateData<V>>

packages/firestore/src/lite/reference_impl.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
CollectionReference,
4242
doc,
4343
DocumentReference,
44-
NestedPartialWithFieldValue,
44+
NestedPartial,
4545
Query,
4646
SetOptions,
4747
TypedUpdateData,
@@ -74,7 +74,7 @@ import { AbstractUserDataWriter } from './user_data_writer';
7474
*/
7575
export function applyFirestoreDataConverter<T>(
7676
converter: UntypedFirestoreDataConverter<T> | null,
77-
value: T,
77+
value: WithFieldValue<T> | NestedPartial<T>,
7878
options?: PublicSetOptions
7979
): PublicDocumentData {
8080
let convertedValue;
@@ -85,7 +85,7 @@ export function applyFirestoreDataConverter<T>(
8585
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8686
convertedValue = (converter as any).toFirestore(value, options);
8787
} else {
88-
convertedValue = converter.toFirestore(value);
88+
convertedValue = converter.toFirestore(value as WithFieldValue<T>);
8989
}
9090
} else {
9191
convertedValue = value as PublicDocumentData;
@@ -220,18 +220,18 @@ export function setDoc<T>(
220220
*/
221221
export function setDoc<T>(
222222
reference: DocumentReference<T>,
223-
data: NestedPartialWithFieldValue<T>,
223+
data: NestedPartial<T>,
224224
options: SetOptions
225225
): Promise<void>;
226226
export function setDoc<T>(
227227
reference: DocumentReference<T>,
228-
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
228+
data: WithFieldValue<T> | NestedPartial<T>,
229229
options?: SetOptions
230230
): Promise<void> {
231231
reference = cast<DocumentReference<T>>(reference, DocumentReference);
232232
const convertedValue = applyFirestoreDataConverter(
233233
reference.converter,
234-
data,
234+
data as WithFieldValue<T>,
235235
options
236236
);
237237
const dataReader = newUserDataReader(reference.firestore);
@@ -376,7 +376,7 @@ export function deleteDoc(
376376
*/
377377
export function addDoc<T>(
378378
reference: CollectionReference<T>,
379-
data: T
379+
data: WithFieldValue<T>
380380
): Promise<DocumentReference<T>> {
381381
reference = cast<CollectionReference<T>>(reference, CollectionReference);
382382
const docRef = doc(reference);

packages/firestore/src/lite/snapshot.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { FieldPath } from './field_path';
2727
import {
2828
DocumentData,
2929
DocumentReference,
30-
NestedPartialWithFieldValue,
30+
NestedPartial,
3131
Query,
3232
queryEqual,
3333
SetOptions,
@@ -93,10 +93,7 @@ export interface FirestoreDataConverter<T> {
9393
* Firestore database). Used with {@link @firebase/firestore/lite#(setDoc:1)}, {@link @firebase/firestore/lite#(WriteBatch.set:1)}
9494
* and {@link @firebase/firestore/lite#(Transaction.set:1)} with `merge:true` or `mergeFields`.
9595
*/
96-
toFirestore(
97-
modelObject: NestedPartialWithFieldValue<T>,
98-
options: SetOptions
99-
): DocumentData;
96+
toFirestore(modelObject: NestedPartial<T>, options: SetOptions): DocumentData;
10097

10198
/**
10299
* Called by the Firestore SDK to convert Firestore data into an object of

packages/firestore/src/lite/transaction.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { Firestore } from './database';
2929
import { FieldPath } from './field_path';
3030
import {
3131
DocumentReference,
32-
NestedPartialWithFieldValue,
32+
NestedPartial,
3333
SetOptions,
3434
TypedUpdateData,
3535
UpdateData,
@@ -135,12 +135,12 @@ export class Transaction {
135135
*/
136136
set<T>(
137137
documentRef: DocumentReference<T>,
138-
data: NestedPartialWithFieldValue<T>,
138+
data: NestedPartial<T>,
139139
options: SetOptions
140140
): this;
141141
set<T>(
142142
documentRef: DocumentReference<T>,
143-
value: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
143+
value: WithFieldValue<T> | NestedPartial<T>,
144144
options?: SetOptions
145145
): this {
146146
const ref = validateReference(documentRef, this._firestore);

packages/firestore/src/lite/user_data_reader.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ import { Firestore } from './database';
6363
import { FieldPath } from './field_path';
6464
import { FieldValue } from './field_value';
6565
import { GeoPoint } from './geo_point';
66-
import {
67-
DocumentReference,
68-
NestedPartialWithFieldValue,
69-
WithFieldValue
70-
} from './reference';
66+
import { DocumentReference, NestedPartial, WithFieldValue } from './reference';
7167
import { Timestamp } from './timestamp';
7268

7369
const RESERVED_FIELD_REGEX = /^__.*__$/;
@@ -78,10 +74,7 @@ const RESERVED_FIELD_REGEX = /^__.*__$/;
7874
*/
7975
export interface UntypedFirestoreDataConverter<T> {
8076
toFirestore(modelObject: WithFieldValue<T>): DocumentData;
81-
toFirestore(
82-
modelObject: NestedPartialWithFieldValue<T>,
83-
options: SetOptions
84-
): DocumentData;
77+
toFirestore(modelObject: NestedPartial<T>, options: SetOptions): DocumentData;
8578
fromFirestore(snapshot: unknown, options?: unknown): T;
8679
}
8780

packages/firestore/src/lite/write_batch.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { Firestore } from './database';
2727
import { FieldPath } from './field_path';
2828
import {
2929
DocumentReference,
30-
NestedPartialWithFieldValue,
30+
NestedPartial,
3131
SetOptions,
3232
TypedUpdateData,
3333
UpdateData,
@@ -91,12 +91,12 @@ export class WriteBatch {
9191
*/
9292
set<T>(
9393
documentRef: DocumentReference<T>,
94-
data: NestedPartialWithFieldValue<T>,
94+
data: NestedPartial<T>,
9595
options: SetOptions
9696
): WriteBatch;
9797
set<T>(
9898
documentRef: DocumentReference<T>,
99-
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
99+
data: WithFieldValue<T> | NestedPartial<T>,
100100
options?: SetOptions
101101
): WriteBatch {
102102
this._verifyNotCommitted();

packages/firestore/test/integration/api/database.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
withTestDocAndInitialData
3333
} from '../util/helpers';
3434
import { DEFAULT_SETTINGS, DEFAULT_PROJECT_ID } from '../util/settings';
35-
import { NestedPartialWithFieldValue } from '../../../src/lite/reference';
35+
import { NestedPartial } from '../../../src/lite/reference';
3636

3737
use(chaiAsPromised);
3838

@@ -1352,7 +1352,7 @@ apiDescribe('Database', (persistence: boolean) => {
13521352

13531353
const postConverterMerge = {
13541354
toFirestore(
1355-
post: NestedPartialWithFieldValue<Post>,
1355+
post: NestedPartial<Post>,
13561356
options?: firestore.SetOptions
13571357
): firestore.DocumentData {
13581358
if (options && (options.merge || options.mergeFields)) {

packages/firestore/test/lite/helpers.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
CollectionReference,
2828
DocumentReference,
2929
SetOptions,
30-
NestedPartialWithFieldValue
30+
NestedPartial
3131
} from '../../src/lite/reference';
3232
import { setDoc } from '../../src/lite/reference_impl';
3333
import { FirestoreSettings } from '../../src/lite/settings';
@@ -124,10 +124,7 @@ export const postConverter = {
124124
};
125125

126126
export const postConverterMerge = {
127-
toFirestore(
128-
post: NestedPartialWithFieldValue<Post>,
129-
options?: SetOptions
130-
): DocumentData {
127+
toFirestore(post: NestedPartial<Post>, options?: SetOptions): DocumentData {
131128
if (
132129
options &&
133130
((options as { merge: true }).merge ||

0 commit comments

Comments
 (0)