TypeScript QueryHelpers return any instead of model type
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.5.3
Node.js version
16.3.2
MongoDB server version
5.0.11
Description
I implemented a query helper function following the tutorial documentation in the documentation.
As soon as I add this method, the return value of that query turns into any
. It should return the proper model type instead.
Steps to Reproduce
Implement a query helper as outlined in the documentation: https://mongoosejs.com/docs/typescript/query-helpers.html
Expected Behavior
The return type after calling the query helper function should be the appropriate model type.
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top Results From Across the Web
Mongoose custom query helpers in TypeScript - Stack Overflow
(schema as any).query.excludeDeleted = function(this: Query<MyDocument>) { return this.where({ isDeleted: false }) };.
Read more >ReturnModelType<T, QueryHelpers> - typegoose
U Required, AnyParamConstructor<any>, The type of a Class to get a Model type of. QueryHelpers, QueryHelpers, Add Query Helpers to the type ......
Read more >Query Helpers in TypeScript - Mongoose
byName = function(name) { return this.find({ name: name }); }; var Project = mongoose.model('Project', ProjectSchema); // Works. Any Project query, whether ...
Read more >Mongoose, Typescript and Async/Await | by Jay Kariesch
In any case, I'll walk through writing schema, a model… ... ITodoDoc extends Mongoose's Document type — which is the standard return value...
Read more >Strongly Typed Mongoose Models - DEV Community
Tagged with typescript, node, mongodb, javascript. ... they call them models while the schema is just the actual format that every document ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
re https://github.com/Automattic/mongoose/issues/12347#issuecomment-1230463627
from what i can tell, this issue is about the documentation because “you followed the documentation, but its not working” and after having taken a look at the documentation and the current code in mongoose’s actual types, i came to the conclusion that the documentation is probably outdated
reason for the documentation being outdated: since the documentation has been written
QueryWithHelpers
has been added, which should make the types easier & cleaner, seehttps://github.com/Automattic/mongoose/blob/0af4cb815ec36dd50d18755f5238506ef9297a5a/types/query.d.ts#L24
by taking care of the
Query<...> & Type
also the documentation lists the first parameter as a static
any
toQuery<any, ...>
, which is the result type, and so ending up in the wrong typeTL;DR: change your type to
QueryWithHelpers
for less code, and also change the first parameter fromany
to the actual return typecurrently the documentation is basically guiding to be like (like in issue #12347) https://github.com/Automattic/mongoose/blob/6cb1284821405c71a4d75e9857ef45d95c367972/types/models.d.ts#L405
here it should actually be
ResultDoc
or{}
and definitely notany
, seehttps://github.com/Automattic/mongoose/blob/0af4cb815ec36dd50d18755f5238506ef9297a5a/types/query.d.ts#L172
https://github.com/Automattic/mongoose/blob/0af4cb815ec36dd50d18755f5238506ef9297a5a/types/query.d.ts#L182-L184
TL;DR: if you are following the documentation, then you will be guaranteed to get
any
as a result after doingquery.exec()
(orquery.then
or implicitly likeawait query
)Fixed by #12507