-
Notifications
You must be signed in to change notification settings - Fork 938
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
Changes from 1 commit
1450b76
8af2407
4f84138
ea1cbd6
e219327
b03e7a3
f2acddc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "a wrappers" should just be "wrappers" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. thanks for catching |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should technically be |
||
/** | ||
* 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1286,7 +1286,7 @@ describe('withConverter() support', () => { | |
} | ||
}; | ||
|
||
describe('NestedPartial', () => { | ||
describe('nested partial support', () => { | ||
const testConverterMerge = { | ||
toFirestore( | ||
testObj: PartialWithFieldValue<TestObject>, | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to log here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.