question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript QueryHelpers return any instead of model type

See original GitHub issue

Prerequisites

  • 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:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
hasezoeycommented, Aug 29, 2022

re https://github.com/Automattic/mongoose/issues/12347#issuecomment-1230463627

but the referenced (my) issue is not about the documentation

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, see

https://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 to Query<any, ...>, which is the result type, and so ending up in the wrong type

TL;DR: change your type to QueryWithHelpers for less code, and also change the first parameter from any to the actual return type


currently 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 not any, see

https://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 doing query.exec() (or query.then or implicitly like await query)

0reactions
vkarpov15commented, Oct 3, 2022

Fixed by #12507

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found