You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the feature has not already been requested
馃殌 Feature Proposal
Hello,
I am building my repository using Typescript 4.9.5 and mongoose 7.0.0
I want to deduce the return type of my function from the lean option :
If true, I want to return Leandocument (the type I put in my generic schema) otherwise HydratedDocument.
Because of the boolean | any type this can't be done correctly.
But if you only put the boolean type (and don't restrict with any), it works fine!
I will show you this in the example above.
Motivation
To infer the return type of a method.
Example
// BlogRepositoryimport{FilterQuery,HydratedDocument,Model,ProjectionFields,QueryOptions}from"mongoose";import{Blog,BlogModel}from"./blog.schema";classBlogRepository{privatereadonlyblogModel: Model<Blog>;constructor(){this.blogModel=BlogModel;}findOne<ProjectionextendsProjectionFields<Blog>,OptionsextendsQueryOptions<Blog>>(filter: FilterQuery<Blog>,projection: Projection,options: Options): Promise<Options['lean']extendstrue
? Pick<Blog,Extract<keyofProjection,keyofBlog>>|null
: HydratedDocument<Pick<Blog,Extract<keyofProjection,keyofBlog>>>|null>{returnthis.blogModel.findOne(filter,projection,options);}}asyncfunctionmain(){constblogRepository=newBlogRepository();constblog=awaitblogRepository.findOne({title: "test"},{content: 1},{lean: true}// or { lean: false });// blog: Document | null instead of type Blog// My blog is therefore polluted by unwanted methods like $where ...}
// node_modules/mongoose/types/query.d.tsinterfaceQueryOptions<DocType=unknown>extendsPopulateOption,SessionOption{
...
...
/** * If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. */lean?: boolean|any;<---HERE!!!pleaseremove**|any**
...
...
}
The text was updated successfully, but these errors were encountered:
FantinRaimbault
added
enhancement
This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
new feature
This change adds new functionality, like a new method or class
labels
Mar 7, 2023
vkarpov15
added
typescript
Types or Types-test related issue / Pull Request
and removed
new feature
This change adds new functionality, like a new method or class
enhancement
This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
labels
Mar 8, 2023
Prerequisites
馃殌 Feature Proposal
Hello,
I am building my repository using Typescript 4.9.5 and mongoose 7.0.0
I want to deduce the return type of my function from the lean option :
If true, I want to return Leandocument (the type I put in my generic schema) otherwise HydratedDocument.
Because of the boolean | any type this can't be done correctly.
But if you only put the boolean type (and don't restrict with any), it works fine!
I will show you this in the example above.
Motivation
To infer the return type of a method.
Example
The text was updated successfully, but these errors were encountered: