Skip to content

types: correct this parameter for methods with versionKey - #16344

Merged
vkarpov15 merged 3 commits into
masterfrom
vkarpov15/gh-16046-3
Jun 30, 2026
Merged

types: correct this parameter for methods with versionKey#16344
vkarpov15 merged 3 commits into
masterfrom
vkarpov15/gh-16046-3

Conversation

@vkarpov15

Copy link
Copy Markdown
Collaborator

Fix #16046

Summary

Omit this from methods because of versionKey option handling mismatch. Schema.create() already does this with the original fix for #16046 here.

Mongoose adds this parameter to methods with AddThisParameter<> so the method body gets type checked correctly in new Schema() (this is correctly set to the inferred document), but then Mongoose strips this when the methods are added to the actual hydrated document type, because at that point TypeScript already knows the correct type of this (the actual document).

Examples

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adjusts Mongoose’s TypeScript typings so hydrated document method types don’t retain an explicit this parameter, avoiding this-context incompatibilities that can show up when schema options like versionKey affect inferred document/model types (per #16046).

Changes:

  • Introduces HydratedDocumentOverrides<> to strip this parameters from function-valued overrides.
  • Applies HydratedDocumentOverrides<> when composing HydratedDocument<> with overrides.
  • Extends type tests to cover versionKey + methods scenarios and ensure method return types are preserved.

Reviewed changes

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

File Description
types/utility.d.ts Adds helper mapped types to omit this from function properties in overrides.
types/index.d.ts Wraps HydratedDocument override composition with HydratedDocumentOverrides<>.
test/types/schema.test.ts Updates the existing gh-16046 type regression to include versionKey and assert method return typing on hydrated docs.
test/types/models.test.ts Adds a second gh-16046 type regression covering schema methods with custom versionKey.

@AbdelrahmanHafez AbdelrahmanHafez added the typescript Types or Types-test related issue / Pull Request label Jun 28, 2026
Comment thread types/utility.d.ts
@vkarpov15
vkarpov15 merged commit 90dd9ea into master Jun 30, 2026
5 checks passed
@hasezoey
hasezoey deleted the vkarpov15/gh-16046-3 branch June 30, 2026 14:43
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.

TypeScript this Context Error When Calling Static Method on Schema Using timestamps and SchemaOptions.virtuals

4 participants