Skip to content

types: cast primitive types within unions when applying create casting - #16320

Merged
vkarpov15 merged 1 commit into
masterfrom
vkarpov15/gh-16316
Jun 4, 2026
Merged

types: cast primitive types within unions when applying create casting#16320
vkarpov15 merged 1 commit into
masterfrom
vkarpov15/gh-16316

Conversation

@vkarpov15

Copy link
Copy Markdown
Collaborator

Fix #16316

Summary

PopulatedDoc is a union of an object type and a TreatAsPrimitives type. create() parameter casting needs to handle both the object type and the primitive type.

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 Model#create() input casting types so union fields that include “primitive-ish” types (notably Types.ObjectId via PopulatedDoc) also accept the corresponding castable primitives (like string) during create casting. This addresses the reported regression where PopulatedDoc<User> could not accept a string id when creating documents.

Changes:

  • Extend ApplyBasicCreateCasting<T> to include QueryTypeCasting for the TreatAsPrimitives portion of union-typed properties (fixing PopulatedDoc-style unions).
  • Add a type test ensuring create() accepts string for a PopulatedDoc<User> ref field.

Reviewed changes

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

File Description
types/models.d.ts Updates create-casting mapped type to allow primitive casting for the TreatAsPrimitives part of union-typed fields (e.g., allowing string for ObjectId in PopulatedDoc).
test/types/create.test.ts Adds a type-level test case covering Model#create() with a PopulatedDoc ref field accepting a string id.

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Jun 4, 2026
@vkarpov15
vkarpov15 merged commit 5028e42 into master Jun 4, 2026
6 checks passed
@hasezoey
hasezoey deleted the vkarpov15/gh-16316 branch June 4, 2026 17:39
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.

string cannot be used for PopulatedDoc<T, ObjectId> (regression)

3 participants