Skip to content

[Types] Schema.virtual() doesn't use THydratedDocumentType #15516

Closed
@simon-abbott

Description

@simon-abbott

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.16.1

Node.js version

v22.15.0

MongoDB server version

8.0.10

Typescript version (if applicable)

5.8.3

Description

The Schema.virtual() method calculates a new HydratedDocument type instead of using THydratedDocumentType. This makes it impossible to use Schema.virtual() with a custom THydratedDocumentType as the types don't line up.

Steps to Reproduce

Make a new type:

type RawType = /** some type */;
type ModelType = /** some model type */;
type DocType = /** some document type */;

const schema = new mongoose.Schema<
  RawType,
  ModelType,
  any,
  any,
  any,
  any,
  GetSchemaOptions<RawType>,
  any,
  DocType
>();

Trying to call the virtual method will then result in it returning VirtualType<any>, not VirtualType<DocType>.

Expected Behavior

Calling Schema.virtual() should return VirtualType<THydratedDocumentType> by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes 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