Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

Fix #15900

Summary

An unintentional backwards breaking change in Mongoose 9 when we removed id as a property on Document: people who use new Schema<RawDocType>() no longer get the id virtual by default. Works fine with automatic inference new Schema(), but once you override RawDocType the id virtual goes away.

This PR adds the id virtual if TOverrides is Record<string, never>, which is the default. Unless id: false option is set, or the RawDocType has an id property.

Examples

…t<> if TOverrides is Record<string, never>

Fix #15900
Copy link
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 backwards compatibility issue in Mongoose 9 where users who explicitly specify a raw document type using new Schema<RawDocType>() no longer get the default id virtual getter, which was unintentionally removed when id was removed as a property on Document.

Key Changes:

  • Modified the HydratedDocument type to add the id virtual by default when TOverrides is Record<string, never> (the default value)
  • Added type tests to verify the id virtual is available when using model<T>() with explicit generics, and that it respects existing id properties

Reviewed changes

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

File Description
types/index.d.ts Added AddDefaultId<HydratedDocPathsType, {}, TSchemaOptions> to the HydratedDocument type when TOverrides is the default empty record, ensuring the id virtual getter is included by default
test/types/document.test.ts Added test function to verify the id virtual is available with explicit generic types and that it doesn't override existing id properties

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

Copy link
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, aside from the test name

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Dec 20, 2025
@vkarpov15 vkarpov15 merged commit 15fc3a7 into master Dec 21, 2025
5 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15900 branch December 21, 2025 18:04
@vkarpov15 vkarpov15 modified the milestones: 9.0.3, 9.1 Dec 30, 2025
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.

The id field is missing when assigning a type to the Model.

4 participants