Skip to content

Ref<T> property cant be filtered with string or ObjectId in v13 #1022

@angelhodar

Description

@angelhodar

Versions

  • System: Windows
  • NodeJS: 24.10.0
  • Typescript: 5.9.3
  • Compiler / Transpiler: tsc / ts-node
  • Typegoose(NPM): 13.0.0
  • mongoose: 9.0.2

What is the Problem?

When upgrading to Typegoose 13 (which uses Mongoose 9), filtering by a Ref<T> field using a string, ObjectId, or even the _id property from a document causes a TypeScript compilation error. In previous versions, these types were accepted in filter queries.

The specific errors encountered are:

No overload matches this call.
  Overload 1 of 4, '(filter?: _QueryFilter<{ name: string; user: User; "user.name": never; "user.email": never; }> | undefined, projection?: ProjectionType<Campaign> | null | undefined, options?: (QueryOptions<...> & Abortable) | undefined): QueryWithHelpers<...>', gave the following error.
    Type 'ObjectId' is not assignable to type 'Condition<ApplyBasicQueryCasting<User>> | undefined'.
      Type 'ObjectId' is not assignable to type 'FilterOperators<User | User[] | null>'.
        Types of property 'id' are incompatible.
          Type 'Uint8Array<ArrayBufferLike>' is not assignable to type 'undefined'.
  Overload 2 of 4, '(filter?: Query<any, any, {}, unknown, "find", Record<string, never>> | undefined, projection?: ProjectionType<Campaign> | null | undefined, options?: (QueryOptions<...> & Abortable) | undefined): QueryWithHelpers<...>', gave the following error.
    Object literal may only specify known properties, and 'user' does not exist in type 'Query<any, any, {}, unknown, "find", Record<string, never>>'.

This breaks common patterns like filtering by IDs received from external APIs (as strings) or using standard ObjectId instances for querying.

Code Example

Full reproduction repository: https://github.com/angelhodar/typegoose-13-repro

Do you know why it happens?

It appears to be a combination of Typegoose 13's stricter Ref<T> type definition and Mongoose 9's transition to QueryFilter (which replaced FilterQuery). The Ref<T> type doesn't seem to include string or Types.ObjectId in its valid query values, even though Mongoose accepts these types correctly at runtime. This creates a mismatch between TypeScript's type checking and Mongoose's actual runtime behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions