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

Type error occurs while pushing embedded subdocument into array #14940

Closed
2 tasks done
claudioaldrighettiatroos opened this issue Oct 8, 2024 · 1 comment
Closed
2 tasks done
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@claudioaldrighettiatroos

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

Node.js version

20.10.0

MongoDB server version

6.0.2

Typescript version (if applicable)

5.5.4

Description

My project has a complex database schema and some document models have arrays of embedded subdocuments. Usually, to update an embedded array the set method of the parent document is used:

const parentInstance = await ParentModel.findById(parentId);

// Some code

parentInstance.set({
  items: [
    ...parentInstance.items,
    newItem1,
    newItem2,
    // Other items...
    // Or it could also be "...newItems" where newItems is an array of new embedded documents
  ],
});

Due to complex updating methods, at some points it happens that it is necessary to use one or more Array prototype methods (for instance push or splice) to build, step by step, the new embedded array subdocument, instead of using just the set method like in the previous simple example. So, in some cases, the code can be (inside a loop for instance):

const parentInstance = await ParentModel.findById(parentId);

// Some complex code

const newItem = { /* new item fields */ };

// Push new item

parentInstance.items.push(newItem);

// Or insert new item inside the embedded array

const insertIndex = parentInstance.items.findIndex((item) => /* some criteria */);

parentInstance.items.splice(insertIndex, 0, newItem);

The problem occurs on cases that are similar to the second example. The type error that occurs on methods like push and splice is the following:

Argument of type '{ /* Item props list... */ }' is not assignable to parameter of type 'Subdocument<unknown, Record<string, never>, IItemModel> & Omit<IItemModel & { _id: ObjectId; }, keyof ItemModelDocumentOverrides> & ItemModelDocumentOverrides'.
  Type '{ /* Item props list... */ }' is missing the following properties from type 'Subdocument<unknown, Record<string, never>, IItemModel>': $isSingleNested, ownerDocument, parent, $parent, and 54 more. ts(2345)

Here's a screenshot of the reproduction code:

Image

In this case, embedded document model has only a code field, thus the error is not due to missing required props.

Am I doing something wrong? Am I missing something on the model interface definition, documentOverrides, virtuals etc...?

On the reproduction link, you can find more details about how documents and embedded subdocuments are defined in my project.

Steps to Reproduce

Reproduction link: Click Here

Expected Behavior

To push or insert new embedded documents into the array without type errors.

@vkarpov15 vkarpov15 added this to the 8.7.3 milestone Oct 18, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Oct 18, 2024
@vkarpov15
Copy link
Collaborator

Your ParentDocumentOverrides type is incorrect, parentInstance.items should be a Mongoose DocumentArray, not a vanilla JavaScript array. Your code compiles fine with the following change:

type ParentDocumentOverrides = {
  items: Types.DocumentArray<ChildInstance>;
};

@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 Oct 18, 2024
@vkarpov15 vkarpov15 removed this from the 8.7.3 milestone Oct 18, 2024
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