types(document): respect schema toObject and toJSON options - #16431
Merged
vkarpov15 merged 1 commit intoAug 5, 2026
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #15594
At runtime,
toObject()andtoJSON()apply the options declared in the schema definition as defaults:$toObjectmerges_defaultToObjectOptionsunder the call options. The declared return types did not reflect that. Options passed at the call site have influenced the return type since #15578, but the no argument overloads always returned the plain document shape, soflattenObjectIds,versionKey: falseorvirtuals: truedeclared at the schema level were invisible to TypeScript.This change routes the no argument overloads, including the populated document variants, through the existing
ToObjectReturnTypemachinery. A newSchemaDeclaredToObjectOptionshelper extracts thetoObject/toJSONoptions captured inTSchemaOptions, andDefaultToObjectReturnTypeshort circuits to the exact previous return type when the schema declares no options for the method, so schemas without declared options see no type change at all.Examples
One note on the snippet in #15594: it constructs the schema as
new Schema<ASchema>(definition, options). TypeScript has no partial inference, so an explicit raw doc type generic preventsTSchemaOptionsfrom capturing the options literal, for this feature the same way as fortimestampstyping. The schema level options propagate on the inference paths:new Schema(definition, options)without explicit generics,Schema.create, or an explicitly suppliedTSchemaOptionsgeneric.New type tests (gh15594) cover a schema with declared options, the unchanged shape for schemas without declared options, call site precedence, a schema declaring only
toJSONoptions, and a custom version key combined withtoObject: { versionKey: false }.npm run test:typespasses with 40 files and 1049 assertions.npm run lint-tsis clean.