Skip to content

fix(types): fix toObject() type inference with timestamps + virtuals#15975

Merged
vkarpov15 merged 2 commits into9.2from
fix-15965
Jan 23, 2026
Merged

fix(types): fix toObject() type inference with timestamps + virtuals#15975
vkarpov15 merged 2 commits into9.2from
fix-15965

Conversation

@AbdelrahmanHafez
Copy link
Copy Markdown
Collaborator

Fixes #15965

When a schema uses both timestamps: true and virtuals, the toObject() return type loses precision. Fields that should be typed (string, subdocs, etc.) degrade to {} | undefined. This doesn’t happen if either timestamps or virtuals is removed.

The virtuals option in SchemaOptions was typed via a virtual getter/setter signature that used an explicit this parameter. That explicit this interacts poorly with inference once timestamps extends the document type. The net effect is that the virtuals option object gets inferred as the definition object instead of its value shape, which then contaminates SchemaOptions and collapses the toObject() return type.

Fix

  • Move this typing onto the virtuals options object using ThisType.
  • Remove the explicit this parameter from the virtual getter/setter type, so inference stays stable.
  • Add a tsd regression test that matches the issue repro and asserts correct toObject() field types with timestamps + virtuals.

I started this on 9.2 because #15864 already reworked relevant types, keeping the fix on 9.2 avoids having to solve it again for 9.2.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TypeScript type inference issue where using both timestamps: true and virtuals in schema options causes toObject() return types to lose precision, degrading typed fields to {} | undefined.

Changes:

  • Removed explicit this parameter from TVirtualPathFN type to prevent inference contamination when combined with timestamps
  • Added ThisType to SchemaOptionsVirtualsPropertyType to maintain this context in virtual getters/setters
  • Added comprehensive type test (gh15965) validating correct type inference for toObject() with timestamps + virtuals

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
types/virtuals.d.ts Refactored virtual path function type to use ThisType instead of explicit this parameter, fixing type inference issues with timestamps
test/types/document.test.ts Added regression test for toObject() type inference with both timestamps and virtuals enabled

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/types/document.test.ts
@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Jan 21, 2026
Copy link
Copy Markdown
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat, I didn't know about ThisType<> in TypeScript - very helpful!

@vkarpov15 vkarpov15 added this to the 9.1.6 milestone Jan 23, 2026
@vkarpov15 vkarpov15 merged commit deec39f into 9.2 Jan 23, 2026
11 checks passed
@hasezoey hasezoey deleted the fix-15965 branch January 24, 2026 12:20
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 this pull request may close these issues.

4 participants