Open
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
7.3.0
Node.js version
16
MongoDB server version
v18.16.0
Typescript version (if applicable)
5.1.3
Description
We have recently upgrade Mongoose from v7.0.3 -> v7.3.0 and noticed that calls to lean are now returning the Model type wrapped with FlattenMaps.
For example we we previously had a model typed in the following way -
// Model
const document: Model<Quote>;
// Example Repository Usage
public async getById(id: string): Promise<Quote> {
const quote = await this.document .findById(id).lean().exec();
// This used to be of type Quote but now FlattenMaps<Quote> and causes a Typescript error
return quote;
}
Since v7.3.0 we have over 100 typescript errors complaining -
// Quote her is just an example it can be replaced with T
Type '(FlattenMaps<Quote> & Required<{ _id: string; }>)[]' is not assignable to type 'Quote[]'
To enable us to build we to do the following -
// Model
const document: Model<Quote>;
// Example Repository Usage
public async getById(id: string): Promise<Quote> {
const quote = await this.document .findById(id).lean<Quote>().exec();
// This would be of type Quote now
return quote;
}
Steps to Reproduce
Please see description above
Expected Behavior
The expected behaviour is that calls to to lean with be type to the T used by model unless overridden like I have done in my example above.