When the _id
field is specified in the schema definition, the type of _id
field cannot be correctly inferred in the HydratedDocument, perhaps due to incorrect implemention of Require_id
#12070
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
Prerequisites
Mongoose version
6.4.3
Node.js version
16.15.1
MongoDB server version
This issue have no relevance to MongoDB server.
Description
When the
_id
field is specified in the schema definition, eg,new Schema({_id: String})
, the type of_id
field cannot be correctly inferred in theHydratedDocument
(the type returned bynew Model
orawait Model.findOne
).Please see the "Steps to Reproduce" section for reproducing code.
I think that the bug is perhaps due to incorrect implemention of
Require_id
:mongoose/types/index.d.ts
Lines 111 to 115 in 3445d24
Sorry that I'm not excellent in Typescript, but what does
U extends any
here mean?As far as I know, everything extends any? so the contiditional clause (T & { _id: Types.ObjectId }) is always used, and the clause T & Required<{ _id: U }> is never used.
Thus,
_id
will be always infered with type ObjectId, even if I have defined another type such as String in the Schema definition.The issue have been previously discussed at e0d299f#r915551078 with @mohammad0-0ahmad @Uzlopak, please see the link for details.
Steps to Reproduce
The following test case demonstrates the bug:
when testing it with
tsd
, gives the following error:Expected Behavior
No response
The text was updated successfully, but these errors were encountered: