Typescript: Unable to correctly represent subdocument array _id fields, other default properties #10947
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
Do you want to request a feature or report a bug?
bug
What is the current behavior?
When trying to represent an array of subdocuments in a schema, you have to include the "_id" field in order for that field to exist in the type of the query response. However, including that field in the type means that any "update" call needs to provide an _id for the update to work.
Any other field which has a default on the subdocument array is also required to be provided when pushing new documents to an array
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
I'm actually unclear on whether there's just something I'm doing wrong here, but this case isn't mentioned in the docs. I'd expect that the subdocuments would get their _id field added automatically the way a top-level document does, since in the docs _ids are never explicitly defined in the type.
In my interfaces, the defaulted fields are set to required because that's how they look in the DB. Maybe there's some kind of helper type I'm missing here to represent the embedded array.
When creating a new document, mongoose basically lets you provide anything as input data and doesn't attempt to enforce typing on it (which is by design AFAIK). By that same logic, it seems like "pushing" a new subdocument during an update would need to follow the same logic, since it is creating a new document with defaults applied.
As an aside, the top-level _id field of
Parent
is typeany
, when I would expect that its type should be ObjectIdWhat are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose: 6.0.11
Node: 14.15
The text was updated successfully, but these errors were encountered: