-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
How to type QueryWithHelpers
so that it returns both HydratedDocument
and FlattenMaps
?
#15311
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
Comments
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
Still active |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
Hi @vkarpov15, is there any news in this issue? Thanks |
Here's how you can fix. First, you need to add interface ProjectQueryHelpers {
byName(
name: string
): QueryWithHelpers<
HydratedDocument<IProject, IProjectVirtuals>[],
HydratedDocument<IProject, IProjectVirtuals>,
ProjectQueryHelpers,
IProject // <-- add this
>;
} Second, put const result = await ProjectModel.find()
.where('stars')
.gt(1000)
.byName('mongoose')
.lean(); Putting As a side note, we should do some work to make it so that the following is typed properly. Right now TypeScript types assume the result of the following query is not lean. const result = await ProjectModel.find()
.where('stars')
.gt(1000)
.lean()
.find(); |
Thanks, Valeri, I followed your instructions and now it works! In addition to the last example you wrote, the typing is also incorrect in the following script: instead of typing const result = await ProjectModel
.find()
.transform((docs) => new Map(docs.map((doc) => [doc._id.toString(), doc])))
.lean(); At this point, can we close the issue? Or would you prefer to keep it open as a reminder to improve these typings? |
Prerequisites
Mongoose version
8.12.1
Node.js version
20.10.0
MongoDB version
6.0.2
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
In the example provided in the documentation, the query helper
byName
returns aHydratedDocument
ofProject
. However, if the query islean
, it should consequently return theFlattenMaps
type. How can the typing be corrected?"Reproduction link here.
The text was updated successfully, but these errors were encountered: