From 776e9f2a6c3d1e59e2922a9d9df3b6f14777f7b7 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 6 Feb 2025 18:58:24 -0500 Subject: [PATCH] docs(typescript): recommend using HydratedSingleSubdocument over Types.Subdocument Fix #15211 --- docs/typescript/subdocuments.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/typescript/subdocuments.md b/docs/typescript/subdocuments.md index 49edbb4ca27..797a53fcc12 100644 --- a/docs/typescript/subdocuments.md +++ b/docs/typescript/subdocuments.md @@ -38,9 +38,11 @@ Define a separate `THydratedDocumentType` and pass it as the 5th generic param t `THydratedDocumentType` controls what type Mongoose uses for "hydrated documents", that is, what `await UserModel.findOne()`, `UserModel.hydrate()`, and `new UserModel()` return. ```ts +import { HydratedSingleSubdocument } from 'mongoose'; + // Define property overrides for hydrated documents type THydratedUserDocument = { - names?: mongoose.Types.Subdocument + names?: HydratedSingleSubdocument } type UserModelType = mongoose.Model; @@ -51,6 +53,7 @@ const UserModel = mongoose.model('User', userSchema); const doc = new UserModel({ names: { _id: '0'.repeat(24), firstName: 'foo' } }); doc.names!.ownerDocument(); // Works, `names` is a subdocument! +doc.names!.firstName; // 'foo' ``` ## Subdocument Arrays @@ -81,4 +84,5 @@ const UserModel = model('User', new Schema