Challenges with working on embedded arrays and typing issues #15041
Labels
help wanted
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Milestone
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: