Skip to content

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

Open
1 task done
nikzanda opened this issue Mar 11, 2025 · 6 comments
Open
1 task done
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@nikzanda
Copy link

Prerequisites

  • I have written a descriptive issue title

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 a HydratedDocument of Project. However, if the query is lean, it should consequently return the FlattenMaps type. How can the typing be corrected?"

Reproduction link here.

Image

@nikzanda nikzanda added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Mar 11, 2025
Copy link

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

@github-actions github-actions bot added the Stale label Mar 26, 2025
@nikzanda
Copy link
Author

Still active

@vkarpov15 vkarpov15 removed the Stale label Mar 26, 2025
Copy link

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

@github-actions github-actions bot added the Stale label Apr 10, 2025
@nikzanda
Copy link
Author

Hi @vkarpov15, is there any news in this issue? Thanks

@github-actions github-actions bot removed the Stale label Apr 11, 2025
@vkarpov15
Copy link
Collaborator

Here's how you can fix. First, you need to add IProject as the raw doc type for your ProjectQueryHelpers as follows:

interface ProjectQueryHelpers {
  byName(
    name: string
  ): QueryWithHelpers<
    HydratedDocument<IProject, IProjectVirtuals>[],
    HydratedDocument<IProject, IProjectVirtuals>,
    ProjectQueryHelpers,
    IProject // <-- add this
  >;
}

Second, put byName() before lean() as follows:

  const result = await ProjectModel.find()
    .where('stars')
    .gt(1000)
    .byName('mongoose') 
    .lean();

Putting lean() last makes it easier for byName to not have to handle lean results.

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();

@nikzanda
Copy link
Author

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 Map<string, FlattenMaps<...>>, type directly FlattenMaps<...> because lean() is called after transform() (by swapping transform and lean, everything works correctly).

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants