-
-
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
types(query): make FilterQuery
props resolve to any
for generics support
#14510
Conversation
@vkarpov15 It seems that its a very common pattern which is followed by a lot of devs, so in that case I don't think there's much to be done in this case other than making |
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.
Looks good to me, though asking because i dont fully understand this back-and-forth of the FilterQuery
type (i have only skimmed the issues / PRs):
the Condition
type has been simplified and now does less checking / work to support more general cases because of typescript weirdness, while still trying to avoid the outcome of any
?
Less checking to allow defining generic functions like the following. Any type checking in function findById<ModelType extends {_id: Types.ObjectId | string}>(model: Model<ModelType>, _id: Types.ObjectId | string){
return model.find({_id: _id});
} |
Fix #14473
Re: #14459
Tagging @FaizBShah @sderrow re: #14436, #14397
Summary
It looks like the major reason for
FilterQuery<T>
setting all props toany
is because TypeScript disallows setting an object with a concrete type to an abstract type. This includes generic function parameters, like calling a function withFilterQuery<>
as a parameter whereFilterQuery<>
depends on the generic. For example:From this SO answer: "Never assign a concrete type to a generic type parameter, consider it as read-only!". That includes calling a function with arguments that rely on the generic type parameter. This restriction makes sense because of
never
This whole "generic function for wrapping Mongoose model functions" pattern isn't one that I would personally use, but it seems to be sufficiently common that we've gotten 3 distinct bug reports related to this change #14459, #14473, #14462
Examples