-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Challenges with working on embedded arrays and typing issues #15041
Comments
I have the same problem. I confirm that the given solution to my issue can fix the error with |
First, the correct way to type your document arrays is the following. Use type UserDocumentOverrides = {
hobbies: Types.DocumentArray<HobbyInstance>;
addresses: Types.DocumentArray<AddressInstance>;
}; I'm unable to repro your issue with The following is not expected to work because the type of
|
Re: |
First of all, thank you for checking the problem with I can understand the motivation behind the decision to make The problem with |
types: add splice() to DocumentArray to allow adding partial objects with splice()
We will have to double check what happens at runtime. Mongoose doesn't explicitly implement It is worth mentioning that |
Prerequisites
Mongoose version
8.6.0
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
I find it hard to work with embedded arrays: the documentation is unclear, and I often have to open issues just to understand how to type my models and embedded arrays correctly.
In the reproduction link, you can see the
User
model with two embedded arrays: hobbies and addresses. In theUserDocumentOverrides
type, hobbies are typed withTypes.DocumentArray<HobbyInstance>
, and addresses are typed asAddressInstance[]
. Both approaches have pros and cons: usingTypes.DocumentArray
allows me to push new elements into the array without typing errors, but the other standard array methods are not implemented; using a plain array type likeAddressInstance[]
implements all array methods, but I cannot use methods like push, unshift, or splice because of the missing properties from theHydratedSubdocument
type.There is also a recently closed issue that suggests using
Types.DocumentArray<EmbeddedInstance>
instead ofEmbeddedInstance[]
, but this still does not seem to be the correct way to work with arrays as expected.Below are the examples described above:
User model
Hobby embedded
Address embedded
Given these issues, is there a way to properly type embedded arrays? I still find it very challenging to work with and understand how to use them effectively.
The text was updated successfully, but these errors were encountered: