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

Open
2 tasks done
claudioaldrighettiatroos opened this issue Oct 8, 2024 · 0 comments
Open
2 tasks done

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant