Skip to content

Commit 7a8431b

Browse files
author
Brian Chen
committed
fix updatedata
1 parent 4d747b1 commit 7a8431b

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

packages/firestore/src/lite/reference.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ export type WithFieldValue<T> = T extends Primitive
7676
* reference nested fields within the document. FieldValues can be passed in
7777
* as property values.
7878
*/
79-
export type UpdateData<T> = T extends Primitive
79+
export type UpdateData<T> = T extends undefined
80+
? never
81+
: T extends Primitive
8082
? T
81-
: T extends Map<infer K, infer V>
82-
? Map<UpdateData<K>, UpdateData<V>>
8383
: T extends {}
8484
? { [K in keyof T]?: UpdateData<T[K]> | FieldValue } & NestedUpdateFields<T>
8585
: Partial<T>;

packages/firestore/src/lite/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type NestedUpdateFields<T extends Record<string, unknown>> =
3535
UnionToIntersection<
3636
{
3737
// Check that T[K] extends Record to only allow nesting for map values.
38-
[K in keyof T & string]: T[K] extends Record<string, unknown>
38+
[K in keyof T & string]: T[K] extends Record<string, unknown> | undefined
3939
? // Recurse into the map and add the prefix in front of each key
4040
// (e.g. Prefix 'bar.' to create: 'bar.baz' and 'bar.qux'.
4141
AddPrefixToKeys<K, UpdateData<T[K]>>

packages/firestore/test/lite/integration.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,49 @@ describe('withConverter() support', () => {
15421542
});
15431543
});
15441544

1545+
it('supports optional fields', () => {
1546+
interface TestObjectOptional {
1547+
optionalStr?: string;
1548+
nested?: {
1549+
requiredStr: string;
1550+
};
1551+
}
1552+
1553+
const testConverterOptional = {
1554+
toFirestore(testObj: WithFieldValue<TestObjectOptional>) {
1555+
return { ...testObj };
1556+
},
1557+
fromFirestore(snapshot: QueryDocumentSnapshot): TestObjectOptional {
1558+
const data = snapshot.data();
1559+
return {
1560+
optionalStr: data.optionalStr,
1561+
nested: data.nested
1562+
};
1563+
}
1564+
};
1565+
1566+
return withTestDocAndInitialData(initialData, async docRef => {
1567+
const testDocRef: DocumentReference<TestObjectOptional> =
1568+
docRef.withConverter(testConverterOptional);
1569+
1570+
await updateDoc(testDocRef, {
1571+
optionalStr: 'foo'
1572+
});
1573+
await updateDoc(testDocRef, {
1574+
'optionalStr': 'foo'
1575+
});
1576+
1577+
await updateDoc(testDocRef, {
1578+
nested: {
1579+
requiredStr: 'foo'
1580+
}
1581+
});
1582+
await updateDoc(testDocRef, {
1583+
'nested.requiredStr': 'foo'
1584+
});
1585+
});
1586+
});
1587+
15451588
it('checks for nonexistent fields', () => {
15461589
return withTestDocAndInitialData(initialData, async docRef => {
15471590
const testDocRef: DocumentReference<TestObject> =

0 commit comments

Comments
 (0)