Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove type any from field lean of interface QueryOptions #13142

Closed
2 tasks done
FantinRaimbault opened this issue Mar 7, 2023 · 2 comments 路 Fixed by #13150
Closed
2 tasks done

Remove type any from field lean of interface QueryOptions #13142

FantinRaimbault opened this issue Mar 7, 2023 · 2 comments 路 Fixed by #13150
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@FantinRaimbault
Copy link

Prerequisites

  • I have written a descriptive issue title
  • 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

// BlogRepository

import { FilterQuery, HydratedDocument, Model, ProjectionFields, QueryOptions } from "mongoose";
import { Blog, BlogModel } from "./blog.schema";

class BlogRepository {
    private readonly blogModel: Model<Blog>;

    constructor() {
        this.blogModel = BlogModel;
    }

    findOne<
        Projection extends ProjectionFields<Blog>,
        Options extends QueryOptions<Blog>
    >(
        filter: FilterQuery<Blog>,
        projection: Projection,
        options: Options
    ): Promise<
        Options['lean'] extends true
            ? Pick<Blog, Extract<keyof Projection, keyof Blog>> | null
            : HydratedDocument<Pick<Blog, Extract<keyof Projection, keyof Blog>>> | null
    > {
        return this.blogModel.findOne(filter, projection, options);
    }
}

async function main() {
    const blogRepository = new BlogRepository();
    const blog = await blogRepository.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.ts

interface QueryOptions<DocType = unknown> extends
    PopulateOption,
    SessionOption {
   ...
   ...
    /**
     * If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
     */
    lean?: boolean | any;  <--- HERE !!! please remove **| any** 
    
    ...
    ...
    
  }
@FantinRaimbault 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
Copy link
Collaborator

Would boolean | Record<string, any> work for you as an alternative?

We have boolean | any for plugins like mongoose-lean-virtuals that require you to do Model.find().lean({ virtuals: true })

@FantinRaimbault
Copy link
Author

yes it would be fine !

@vkarpov15 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
@vkarpov15 vkarpov15 added this to the 7.0.2 milestone Mar 8, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Mar 9, 2023
vkarpov15 added a commit that referenced this issue Mar 10, 2023
type(query): changed `QueryOptions` lean type to `Record<string, any>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants