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 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/short-mice-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore': minor
---

Expanded `Firestore.WithFieldValue<T>` to include `T`. This allows developers to delegate `WithFieldValue<T>` inside a wrappers of type `T` to avoid exposing Firebase types beyond Firebase-specific logic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "a wrappers" should just be "wrappers"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. thanks for catching

8 changes: 3 additions & 5 deletions common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ export function orderBy(fieldPath: string | FieldPath, directionStr?: OrderByDir
export type OrderByDirection = 'desc' | 'asc';

// @public
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<WithFieldValue<T>>;

// @public
export type Primitive = string | number | boolean | undefined | null;
Expand Down Expand Up @@ -352,9 +350,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>;
} : Partial<T>);

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

// @public
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<WithFieldValue<T>>;

// @public
export interface PersistenceSettings {
Expand Down Expand Up @@ -504,9 +502,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>;
} : Partial<T>);

// @public
export class WriteBatch {
Expand Down
19 changes: 8 additions & 11 deletions packages/firestore/src/lite-api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,19 @@ 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<WithFieldValue<T>>;

/**
* 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 }
: Partial<T>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but why do we allow Partial here? Does that mean a user can omit properties as long as they are nested?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should technically be never. Fixed.

/**
* Update data (for use with {@link (updateDoc:1)}) that consists of field paths
* (e.g. 'foo' or 'foo.baz') mapped to values. Fields that contain dots
Expand Down
47 changes: 46 additions & 1 deletion 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 @@ -1496,6 +1496,51 @@ describe('withConverter() support', () => {
});
});
});

describe('used as a type', () => {
class ObjectWrapper<T> {
withFieldValueT(value: WithFieldValue<T>): void {
// eslint-disable-next-line no-console
console.log(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to log here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

}

withPartialFieldValueT(value: PartialWithFieldValue<T>): void {
// eslint-disable-next-line no-console
console.log(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