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
TypeScript QueryHelpers return any instead of model type #12342
Comments
i think the mentioned documentation is at least slightly out-of-date, because a proper type for query-helpers should be |
I'm not sure what the correct return type is because I find it a bit confusing, but it should certainly not be |
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: Line 24 in 0af4cb8
by taking care of the also the documentation lists the first parameter as a static TL;DR: change your type to currently the documentation is basically guiding to be like (like in issue #12347) Line 405 in 6cb1284
here it should actually be Line 172 in 0af4cb8
Lines 182 to 184 in 0af4cb8
TL;DR: if you are following the documentation, then you will be guaranteed to get |
Thank you for the explanation. I didn't know that only the docs were wrong. But I'm unsure what to pass instead of
Can you give me any tips here? Also, if I understand it correctly, QueryWithHelpers lets me remove the |
i basically suggested this because mongoose uses that internally over just
the expected return-type (if the query returns a single) should be a document (or document array), which should be done with like (updated documentation): // note the "T" generic is just to store the same result for both the first and second generic for "QueryWithHelpers"
type ProjectModelQuery<T = HydratedDocument<Project>> = QueryWithHelpers<T | null, T, ProjectQueryHelpers>;
// or
type ProjectModelQuery = QueryWithHelpers<HydratedDocument<Project> | null, HydratedDocument<Project>, ProjectQueryHelpers>; |
@hasezoey Thank you so much! I think I tried |
TypeScript query helpers quick fix and docs improvements
Fixed by #12507 |
Prerequisites
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.
The text was updated successfully, but these errors were encountered: