Skip to content

Commit 98bd1ad

Browse files
author
Brian Chen
committed
resolve sebastian comments
1 parent 160423a commit 98bd1ad

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

packages/firestore/src/lite-api/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type Primitive = string | number | boolean | undefined | null;
3434
export type NestedUpdateFields<T extends Record<string, unknown>> =
3535
UnionToIntersection<
3636
{
37-
[K in keyof T & string]: ChildUpdateFields<T[K], K>;
37+
[K in keyof T & string]: ChildUpdateFields<K, T[K]>;
3838
}[keyof T & string] // Also include the generated prefix-string keys.
3939
>;
4040

@@ -43,17 +43,17 @@ export type NestedUpdateFields<T extends Record<string, unknown>> =
4343
* to distribute union types such as `undefined | {...}` (happens for optional
4444
* props) or `{a: A} | {b: B}`.
4545
*
46-
* In this use case, `T1` is used to distribute the union types of `T[K]` on
46+
* In this use case, `V` is used to distribute the union types of `T[K]` on
4747
* `Record`, since `T[K]` is evaluated as an expression and not distributed.
4848
*
4949
* See https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types
5050
*/
51-
export type ChildUpdateFields<T1, K extends string> =
51+
export type ChildUpdateFields<K extends string, V> =
5252
// Only allow nesting for map values
53-
T1 extends Record<string, unknown>
53+
V extends Record<string, unknown>
5454
? // Recurse into the map and add the prefix in front of each key
5555
// (e.g. Prefix 'bar.' to create: 'bar.baz' and 'bar.qux'.
56-
AddPrefixToKeys<K, UpdateData<T1>>
56+
AddPrefixToKeys<K, UpdateData<V>>
5757
: // UpdateData is always a map of values.
5858
never;
5959

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,42 @@ describe('withConverter() support', () => {
15851585
});
15861586
});
15871587

1588+
it('supports null fields', () => {
1589+
interface TestObjectOptional {
1590+
optionalStr?: string;
1591+
nested?: {
1592+
strOrNull: string | null;
1593+
};
1594+
}
1595+
1596+
const testConverterOptional = {
1597+
toFirestore(testObj: WithFieldValue<TestObjectOptional>) {
1598+
return { ...testObj };
1599+
},
1600+
fromFirestore(snapshot: QueryDocumentSnapshot): TestObjectOptional {
1601+
const data = snapshot.data();
1602+
return {
1603+
optionalStr: data.optionalStr,
1604+
nested: data.nested
1605+
};
1606+
}
1607+
};
1608+
1609+
return withTestDocAndInitialData(initialData, async docRef => {
1610+
const testDocRef: DocumentReference<TestObjectOptional> =
1611+
docRef.withConverter(testConverterOptional);
1612+
1613+
await updateDoc(testDocRef, {
1614+
nested: {
1615+
strOrNull: null
1616+
}
1617+
});
1618+
await updateDoc(testDocRef, {
1619+
'nested.strOrNull': null
1620+
});
1621+
});
1622+
});
1623+
15881624
it('supports union fields', () => {
15891625
interface TestObjectUnion {
15901626
optionalStr?: string;
@@ -1612,13 +1648,6 @@ describe('withConverter() support', () => {
16121648
const testDocRef: DocumentReference<TestObjectUnion> =
16131649
docRef.withConverter(testConverterUnion);
16141650

1615-
await updateDoc(testDocRef, {
1616-
optionalStr: 'foo'
1617-
});
1618-
await updateDoc(testDocRef, {
1619-
'optionalStr': 'foo'
1620-
});
1621-
16221651
await updateDoc(testDocRef, {
16231652
nested: {
16241653
requiredStr: 'foo'
@@ -1641,6 +1670,10 @@ describe('withConverter() support', () => {
16411670
// @ts-expect-error
16421671
'nested.requiredNumber': 'foo'
16431672
});
1673+
await updateDoc(testDocRef, {
1674+
// @ts-expect-error
1675+
'nested.requiredNumber': null
1676+
});
16441677
});
16451678
});
16461679

0 commit comments

Comments
 (0)