Why is the default type of the generic field _id in the class Document of type "any"? #14520
Closed
1 task done
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
Prerequisites
Mongoose version
8.3.1
Node.js version
20.12.1
MongoDB version
6.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.2.1 (23C71)
Issue
I just fell victim to an issue similar to:
#11085
Which was caused by legacy code we've been using in NestJS:
nestjs/docs.nestjs.com#2517
After researching this for quite some time, I undersand we are supposed to use the
HydratedDocument
these days, but I'd still be nice if somebody can shade some light into why it is what it is at the moment.So here is my example:
Our schemas are defined like this (which is how it was found in NestJS docs, a few years ago):
The interesting part is here is:
export type CatDocument = Cat & Document;
Which basically makes _id be of type
any
instead ofTypes.ObjectId
.As per above issue, I wonder why document is defined as
class Document<T = any, TQueryHelpers = any, DocType = any>
in https://github.com/Automattic/mongoose/blob/master/types/document.d.ts#L19 while the HydratedDocument was changed to useunknown
to circumvent the problem withany
inference.Wouldn't it make sense to either use
unkown
orclass Document<T = Types.ObjectId, TQueryHelpers = any, DocType = any>
to default to ObjectId and leave the user a way to use any instead?Thanks for any answers to this question.
The text was updated successfully, but these errors were encountered: