Skip to content
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

Unexpected error executing a lean query and passing the result of the query to a function that accepts a variable of that interface type. #15275

Open
2 tasks done
nikzanda opened this issue Feb 26, 2025 · 4 comments
Labels
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
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.10.2

Node.js version

20.10.0

MongoDB server version

6.0.2

Typescript version (if applicable)

5.7.2

Description

I have a model Post that references the model User. The User model includes an embedded field, addresses, which is overridden by the type UserDocumentOverrides using Types.DocumentArray<AddressInstance>. For some reason, this override causes a typing error when performing a lean query on the Post model and passing the query result to a function that expects a parameter of type IPost[].

Steps to Reproduce

Reproduction link here.

P.S. If I locally comment out line 21 of the user.ts file, the error disappears. However, for some reason, on StackBlitz, the error remains.

Expected Behavior

As a workaround, I can manually infer the type during the query .lean<IPost[]>(). However, I don't expect this type of error and having to manually specify the typing.

@vkarpov15 vkarpov15 added this to the 8.11.1 milestone Feb 28, 2025
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Feb 28, 2025
@vkarpov15
Copy link
Collaborator

The following looks like it fixes the issue:

type UserDocumentOverrides = {
  addresses: Types.DocumentArray<IAddress>;
};

You just need to pass the raw document interface to Types.DocumentArray, not the hydrated document interface

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 3, 2025
@nikzanda
Copy link
Author

nikzanda commented Mar 3, 2025

The following looks like it fixes the issue:

type UserDocumentOverrides = {
addresses: Types.DocumentArray;
};
You just need to pass the raw document interface to Types.DocumentArray, not the hydrated document interface

I'm not sure if this is the correct solution. I tried to further complicate the code by adding virtual fields to address and a document override field:

Image

This forces me to type address in Types.DocumentArray as IAddress & IAddressDocumentOverrides & IAddressVirtuals, otherwise a function that accepts AddressInstance as a parameter returns an error if I pass user.addresses. This, in my opinion, is not very elegant to look at or to write:

Image

Additionally, the same error for which I opened this issue remains:

Image

@vkarpov15 vkarpov15 modified the milestones: 8.12.1, 8.12.2 Mar 4, 2025
@vkarpov15
Copy link
Collaborator

In that case, try the following. Does this work for you?

import {
  HydratedArraySubdocument,
} from 'mongoose';
import addressSchema, {
  IAddress,
  IAddressDocumentOverrides,
  IAddressVirtuals,
} from './embedded/address';

type UserDocumentOverrides = {
  addresses: Types.DocumentArray<
    HydratedArraySubdocument<
      IAddress,
      IAddressDocumentOverrides & IAddressVirtuals
    >
  >;
};

@vkarpov15 vkarpov15 removed this from the 8.12.2 milestone Mar 7, 2025
@nikzanda
Copy link
Author

nikzanda commented Mar 7, 2025

Sorry, but the solution you provided to me is not working. I have updated the repository, and you can see that the described error remains the same. Furthermore, your solution is very similar to the initial code I wrote: I typed addresses as Types.DocumentArray<AddressInstance>, where AddressInstance is a HydratedSingleSubdocument, whereas you used HydratedArraySubdocument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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