Skip to content

Allow type T in WithFieldValue<T> #5675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/short-mice-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/firestore': minor
'firebase': minor
---

Expanded `Firestore.WithFieldValue<T>` to include `T`. This allows developers to delegate `WithFieldValue<T>` inside wrappers of type `T` to avoid exposing Firebase types beyond Firebase-specific logic.
8 changes: 4 additions & 4 deletions common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ export function orderBy(fieldPath: string | FieldPath, directionStr?: OrderByDir
export type OrderByDirection = 'desc' | 'asc';

// @public
export type PartialWithFieldValue<T> = T extends Primitive ? T : T extends {} ? {
export type PartialWithFieldValue<T> = Partial<T> | (T extends Primitive ? T : T extends {} ? {
[K in keyof T]?: PartialWithFieldValue<T[K]> | FieldValue;
} : Partial<T>;
} : never);

// @public
export type Primitive = string | number | boolean | undefined | null;
Expand Down Expand Up @@ -352,9 +352,9 @@ export function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value
export type WhereFilterOp = '<' | '<=' | '==' | '!=' | '>=' | '>' | 'array-contains' | 'in' | 'array-contains-any' | 'not-in';

// @public
export type WithFieldValue<T> = T extends Primitive ? T : T extends {} ? {
export type WithFieldValue<T> = T | (T extends Primitive ? T : T extends {} ? {
[K in keyof T]: WithFieldValue<T[K]> | FieldValue;
} : Partial<T>;
} : never);

// @public
export class WriteBatch {
Expand Down
8 changes: 4 additions & 4 deletions common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ export function orderBy(fieldPath: string | FieldPath, directionStr?: OrderByDir
export type OrderByDirection = 'desc' | 'asc';

// @public
export type PartialWithFieldValue<T> = T extends Primitive ? T : T extends {} ? {
export type PartialWithFieldValue<T> = Partial<T> | (T extends Primitive ? T : T extends {} ? {
[K in keyof T]?: PartialWithFieldValue<T[K]> | FieldValue;
} : Partial<T>;
} : never);

// @public
export interface PersistenceSettings {
Expand Down Expand Up @@ -504,9 +504,9 @@ export function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value
export type WhereFilterOp = '<' | '<=' | '==' | '!=' | '>=' | '>' | 'array-contains' | 'in' | 'array-contains-any' | 'not-in';

// @public
export type WithFieldValue<T> = T extends Primitive ? T : T extends {} ? {
export type WithFieldValue<T> = T | (T extends Primitive ? T : T extends {} ? {
[K in keyof T]: WithFieldValue<T[K]> | FieldValue;
} : Partial<T>;
} : never);

// @public
export class WriteBatch {
Expand Down
24 changes: 14 additions & 10 deletions packages/firestore/src/lite-api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,25 @@ export interface DocumentData {
* Similar to Typescript's `Partial<T>`, but allows nested fields to be
* omitted and FieldValues to be passed in as property values.
*/
export type PartialWithFieldValue<T> = T extends Primitive
? T
: T extends {}
? { [K in keyof T]?: PartialWithFieldValue<T[K]> | FieldValue }
: Partial<T>;
export type PartialWithFieldValue<T> =
| Partial<T>
| (T extends Primitive
? T
: T extends {}
? { [K in keyof T]?: PartialWithFieldValue<T[K]> | FieldValue }
: never);

/**
* Allows FieldValues to be passed in as a property value while maintaining
* type safety.
*/
export type WithFieldValue<T> = T extends Primitive
? T
: T extends {}
? { [K in keyof T]: WithFieldValue<T[K]> | FieldValue }
: Partial<T>;
export type WithFieldValue<T> =
| T
| (T extends Primitive
? T
: T extends {}
? { [K in keyof T]: WithFieldValue<T[K]> | FieldValue }
: never);

/**
* Update data (for use with {@link (updateDoc:1)}) that consists of field paths
Expand Down
131 changes: 129 additions & 2 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ describe('withConverter() support', () => {
}
};

describe('NestedPartial', () => {
describe('nested partial support', () => {
const testConverterMerge = {
toFirestore(
testObj: PartialWithFieldValue<TestObject>,
Expand Down Expand Up @@ -1399,6 +1399,44 @@ describe('withConverter() support', () => {
);
});
});

it('allows omitting fields', async () => {
return withTestDoc(async doc => {
const ref = doc.withConverter(testConverterMerge);

// Omit outer fields
await setDoc(
ref,
{
outerString: deleteField(),
nested: {
innerNested: {
innerNestedNum: increment(1)
},
innerArr: arrayUnion(2),
timestamp: serverTimestamp()
}
},
{ merge: true }
);

// Omit inner fields
await setDoc(
ref,
{
outerString: deleteField(),
outerArr: [],
nested: {
innerNested: {
innerNestedNum: increment(1)
},
timestamp: serverTimestamp()
}
},
{ merge: true }
);
});
});
});

describe('WithFieldValue', () => {
Expand All @@ -1421,7 +1459,7 @@ describe('withConverter() support', () => {
});
});

it('requires all fields to be present', async () => {
it('requires all outer fields to be present', async () => {
return withTestDoc(async doc => {
const ref = doc.withConverter(testConverter);

Expand All @@ -1440,6 +1478,24 @@ describe('withConverter() support', () => {
});
});

it('requires all nested fields to be present', async () => {
return withTestDoc(async doc => {
const ref = doc.withConverter(testConverter);

await setDoc(ref, {
outerString: 'foo',
outerArr: [],
// @ts-expect-error
nested: {
innerNested: {
innerNestedNum: increment(1)
},
timestamp: serverTimestamp()
}
});
});
});

it('validates inner and outer fields', async () => {
return withTestDoc(async doc => {
const ref = doc.withConverter(testConverter);
Expand Down Expand Up @@ -1496,6 +1552,77 @@ describe('withConverter() support', () => {
});
});
});

it('allows certain types but not others', () => {
const withTryCatch = async (fn: () => Promise<void>): Promise<void> => {
try {
await fn();
} catch {}
};

// These tests exist to establish which object types are allowed to be
// passed in by default when `T = DocumentData`. Some objects extend
// the Javascript `{}`, which is why they're allowed whereas others
// throw an error.
return withTestDoc(async doc => {
// @ts-expect-error
await withTryCatch(() => setDoc(doc, 1));
// @ts-expect-error
await withTryCatch(() => setDoc(doc, 'foo'));
// @ts-expect-error
await withTryCatch(() => setDoc(doc, false));
await withTryCatch(() => setDoc(doc, undefined));
await withTryCatch(() => setDoc(doc, null));
await withTryCatch(() => setDoc(doc, [0]));
await withTryCatch(() => setDoc(doc, new Set<string>()));
await withTryCatch(() => setDoc(doc, new Map<string, number>()));
});
});

describe('used as a type', () => {
class ObjectWrapper<T> {
withFieldValueT(value: WithFieldValue<T>): WithFieldValue<T> {
return value;
}

withPartialFieldValueT(
value: PartialWithFieldValue<T>
): PartialWithFieldValue<T> {
return value;
}

// Wrapper to avoid having Firebase types in non-Firebase code.
withT(value: T): void {
this.withFieldValueT(value);
}

// Wrapper to avoid having Firebase types in non-Firebase code.
withPartialT(value: Partial<T>): void {
this.withPartialFieldValueT(value);
}
}

it('supports passing in the object as `T`', () => {
interface Foo {
id: string;
foo: number;
}
const foo = new ObjectWrapper<Foo>();
foo.withFieldValueT({ id: '', foo: increment(1) });
foo.withPartialFieldValueT({ foo: increment(1) });
foo.withT({ id: '', foo: 1 });
foo.withPartialT({ foo: 1 });
});

it('does not allow primitive types to use FieldValue', () => {
type Bar = number;
const bar = new ObjectWrapper<Bar>();
// @ts-expect-error
bar.withFieldValueT(increment(1));
// @ts-expect-error
bar.withPartialFieldValueT(increment(1));
});
});
});

describe('UpdateData', () => {
Expand Down