-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Bug/types/update query #11588
Bug/types/update query #11588
Conversation
@@ -2058,7 +2058,7 @@ declare module 'mongoose' { | |||
* { age: 30 } | |||
* ``` | |||
*/ | |||
export type UpdateQuery<T> = _UpdateQuery<_UpdateQueryDef<T>> & AnyObject; | |||
export type UpdateQuery<T> = Partial<T> & _UpdateQuery<_UpdateQueryDef<T>> & AnyObject; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason is the Union Type with AnyObject resulting in a Type Widening. I would usually try with Omit<AnyObject, keyof T & _UpdateQuery<_UpdateQueryDef>> instead of just using AnyObjecy.
export type UpdateQuery<T> = Partial<T> & _UpdateQuery<_UpdateQueryDef<T>> & AnyObject; | |
export type UpdateQuery<T> = Partial<T> & _UpdateQuery<_UpdateQueryDef<T>> & Omit<AnyObject, keyof T & _UpdateQuery<_UpdateQueryDef<T>>>; |
Didn't test it, just wrote it with my mobile.
Also look into the populate typing PR. @mohammad0-0ahmad used a different notation to achieve the correct typings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe the following, if update query should accept any type:
@vkarpov15 what do u think about that ?
export type UpdateQuery<T> = _UpdateQuery<_UpdateQueryDef<T>> & (T | AnyObject);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohammad0-0ahmad that approach works for me, as long as it is & (Partial<T> | AnyObject)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Overall, I think this approach works and is a neat improvement for intellisense. I'll try out @mohammad0-0ahmad 's suggestion after merging.
@@ -2058,7 +2058,7 @@ declare module 'mongoose' { | |||
* { age: 30 } | |||
* ``` | |||
*/ | |||
export type UpdateQuery<T> = _UpdateQuery<_UpdateQueryDef<T>> & AnyObject; | |||
export type UpdateQuery<T> = Partial<T> & _UpdateQuery<_UpdateQueryDef<T>> & AnyObject; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohammad0-0ahmad that approach works for me, as long as it is & (Partial<T> | AnyObject)
Actually, I didn't notice that this is causing test failures, sorry. I'm going to have to revert and we'll have to consider how we can do this without breaking our tests. |
Currently, we can do the following in JavaScript:
But TypeScript doesn't know that the second parameter to
updateOne
can takename
as a top level property.It would, however, understand the types if we use
$set
:I managed to make it read the properties from the interface, but another bug seems to be that it converts all types to
any
instead of the type defined on the schema.