Skip to content

types: allow unknown keys in subdocs (retain autocomplete suggestions) and use expect-type for type assertions#16048

Merged
vkarpov15 merged 7 commits intomasterfrom
vkarpov15/gh-16012
Mar 2, 2026
Merged

types: allow unknown keys in subdocs (retain autocomplete suggestions) and use expect-type for type assertions#16048
vkarpov15 merged 7 commits intomasterfrom
vkarpov15/gh-16012

Conversation

@vkarpov15
Copy link
Copy Markdown
Collaborator

Fix #16012

Summary

Allows passing in extra keys to subdocuments while retaining autocomplete:

image

I decided to allow adding extra keys to subdocuments because adding the full virtuals to subdocuments would require more switches to InferRawDocType, InferSchemaType, etc. and that would be unwieldy. Allowing unknown fields while retaining autocomplete seems like a reasonable middle ground.

While working on this, I also found that our current ExpectType assertions don't handle never well: never is assignable to any type so ExpectType<number>({} as never) unexpectedly succeeds:

image

I wasn't able to come up with a reasonable workaround in ExpectType<> to correctly handle never, so I'm pulling in expect-type npm package to help with that. I made current assertions.ts a wrapper for expect-type for now to avoid massive diffs, but removing assertions.ts in favor of using expect-type directly is a candidate for future work.

Examples

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 pull request addresses TypeScript type inference issues with subdocuments containing virtuals with setters (issue #16012) by allowing unknown keys in subdocuments while preserving autocomplete functionality. The PR also improves type assertions by adopting the expect-type library, which correctly handles the never type that the previous implementation did not.

Changes:

  • Modified type casting logic to allow extra keys in subdocuments using CreateObjectWithExtraKeys<T> = T & Record<string, unknown>
  • Refactored ApplyBasicCreateCasting to check for TreatAsPrimitives before general objects, ensuring correct type handling
  • Replaced type assertion declarations with expect-type library implementation for better type checking
  • Added test case demonstrating the fix for gh16012 with virtual setters in subdocuments

Reviewed changes

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

File Description
types/models.d.ts Adds CreateObjectWithExtraKeys type and updates ApplyBasicCreateCasting to allow unknown keys in subdocuments and nested objects while retaining type safety for known fields
test/types/util/assertions.ts Converts ExpectType and ExpectAssignable from type declarations to runtime functions wrapping expect-type library for improved type assertion handling
test/types/create.test.ts Adds gh16012 test demonstrating that virtuals can now be used in subdocument creation and that expect-type properly handles never type
package.json Adds expect-type as a new dependency for improved type testing

Comment thread package.json Outdated
"type": "commonjs",
"license": "MIT",
"dependencies": {
"expect-type": "^1.3.0",
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The expect-type package should be moved to devDependencies rather than dependencies. This package is only used in TypeScript type tests (under test/types/) and is not needed for the runtime functionality of mongoose or for users of the mongoose package. Type test utilities are development dependencies, not runtime dependencies.

Copilot uses AI. Check for mistakes.
@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Feb 26, 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.

Aside from the dependency location, looks good to me

Comment thread package.json Outdated
Comment thread test/types/create.test.ts Outdated
@vkarpov15 vkarpov15 merged commit 014e9f7 into master Mar 2, 2026
55 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-16012 branch March 2, 2026 12:08
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: Regression in allowing Virtuals with Setters in Document creation/initialization after upgrading to v9

3 participants