model.create does not have a lean option, and using toObject and toJSON does not produce similar typing #15077
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
typescript
Types or Types-test related issue / Pull Request
Prerequisites
Mongoose version
8.8.3
Node.js version
22.11.0
MongoDB server version
7.0
Typescript version (if applicable)
4.9.5
Description
While upgrading to the new 8.8.3 due to a dependabot alert, types broke for my app,
when doing a find, if not found create scenario and reusing the
let
variable, which has a type inferred already, as a LeanDocument (only exists on find/Query type) which is compatible with lots of other types previously, but is not anymore due to the addition of the __v to it, and not intoObject
andtoJSON
which are my usual workarounds to get a type from a newly created Document (Document type)Doing the following workarounds that i usually use does not work anymore, (except casting which i want to avoid):
document.toObject
this used to produce a type identicalTS error using 8.8.3:
Output of toObject:
T & Required<{ _id: mongoose.Types.ObjectId; }>
Output of lean.exec which gives a raw-er datatype (which used to work fine):
{ _id?: mongoose.Types.ObjectId; ... 38 more ...; } & Required<{ _id: mongoose.Types.ObjectId; }> & { __v: number; }
Steps to Reproduce
https://github.com/Amoodaa/mongoose-8-8-type-error
On
8.8.3
:On
8.7.3
: no errors, check readme for moreExpected Behavior
I would expect there would some kind of type similarity between the different lean versions? like toObject
A seamless way to get away from mongoose types and go back to my defined types, or at least type that is very compatible with that,
I've migrated from 6 -> 7 -> 8 recently and have faced many problems with mongoose typing solution, but its obviously better than being blind so i really appreciate your work on typings, thank you so much
The text was updated successfully, but these errors were encountered: