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

DocumentArrays defined in TMethodsAndOverrides do not correctly override Arrays. #13040

Closed
2 tasks done
juona opened this issue Feb 16, 2023 · 0 comments · Fixed by #13066
Closed
2 tasks done

DocumentArrays defined in TMethodsAndOverrides do not correctly override Arrays. #13040

juona opened this issue Feb 16, 2023 · 0 comments · Fixed by #13066
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@juona
Copy link

juona commented Feb 16, 2023

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.9.1

Node.js version

12.22.10

MongoDB server version

5

Typescript version (if applicable)

4.9.5

Description

I was following this example from the official docs: https://mongoosejs.com/docs/typescript/subdocuments.html#subdocument-arrays.

Unfortunately, there is problem in the provided example:

// Given
const doc = new UserModel({});

// This indeed works:
doc.names[0].ownerDocument(); // Works!

// But this does not!
doc.names.forEach(name => {
  name.ownerDocument() // Does not work! no property `ownerDocument`
});

Seems that Array methods, such as forEach, find, etc. are not correctly overridden by DocumentArray.

Steps to Reproduce

Here's a full custom example:

// First I define a few simple types

interface Product {}

interface User {
  products: Product[];
}

// I want my `UserDocument` type to define `products` as a `DocumentArray`; I'll do this using overrides

interface UserOverrides {
  products: Types.DocumentArray<Product>;
}

interface UserModel extends Model<User, {}, UserOverrides> {}

// Here I determine the type of user documents; I could also manually define a `HydratedDocument` - makes no difference.

interface UserDocument extends InstanceType<UserModel> {}

// Assume I have an instance of `UserDocument`

let user: UserDocument;

// This is then fine:

user.products[0].ownerDocument(); // ok

// While this is not:

user.products.forEach(product => {
  product.ownerDocument(); // error - no property named `ownerDocument`
});

If interface User did not have products: Product[] defined, the error would go away. This is not a great solution however.

Expected Behavior

Using Array prototype methods such as forEach, find and others on a DocumentArray override should correctly infer array entry types to be Subdocuments, not POJOs.

@vkarpov15 vkarpov15 added this to the 6.9.3 milestone Feb 17, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Feb 17, 2023
vkarpov15 added a commit that referenced this issue Feb 21, 2023
vkarpov15 added a commit that referenced this issue Feb 21, 2023
fix(types): use MergeTypes for type overrides in HydratedDocument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants