Skip to content

FilterQuery Typing fails for generic type #14459

Closed

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

8.2.3

Node.js version

20

MongoDB server version

7.0.6

Typescript version (if applicable)

5.4.3

Description

Repro Script throws

No overload matches this call.
  Overload 1 of 5, '(filter: FilterQuery<ModelType>, projection?: ProjectionType<ModelType> | null | undefined, options?: QueryOptions<ModelType> | null | undefined): Query<...>', gave the following error.
    Argument of type '{ _id: string | Types.ObjectId; }' is not assignable to parameter of type 'FilterQuery<ModelType>'.
      Type '{ _id: string | Types.ObjectId; }' is not assignable to type '{ [P in keyof ModelType]?: Condition<ModelType[P]> | undefined; }'.
  Overload 2 of 5, '(filter: FilterQuery<ModelType>, projection?: ProjectionType<ModelType> | null | undefined): Query<IfAny<ModelType, any, Document<...> & Require_id<...>>[], IfAny<...>, {}, ModelType, "find">', gave the following error.
    Argument of type '{ _id: string | Types.ObjectId; }' is not assignable to parameter of type 'FilterQuery<ModelType>'.
      Type '{ _id: string | Types.ObjectId; }' is not assignable to type '{ [P in keyof ModelType]?: Condition<ModelType[P]> | undefined; }'.
  Overload 3 of 5, '(filter: FilterQuery<ModelType>): Query<IfAny<ModelType, any, Document<unknown, {}, ModelType> & Require_id<ModelType>>[], IfAny<...>, {}, ModelType, "find">', gave the following error.
    Argument of type '{ _id: string | Types.ObjectId; }' is not assignable to parameter of type 'FilterQuery<ModelType>'.
      Type '{ _id: string | Types.ObjectId; }' is not assignable to type '{ [P in keyof ModelType]?: Condition<ModelType[P]> | undefined; }'.ts(2769)

Steps to Reproduce

import { Model, Types } from "mongoose";

function findById<ModelType extends {_id: Types.ObjectId | string}>(model: Model<ModelType>, _id: Types.ObjectId | string){
    return model.find({_id: _id})
}

Expected Behavior

No Error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessarytypescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions