From 10e83ae693f8923c4b64cb8ca2e317e804370efc Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 24 May 2024 10:03:43 -0400 Subject: [PATCH 1/2] types: pass DocType down to subdocuments so `HydratedSingleSubdocument` and `HydratedArraySubdocument` `toObject()` returns correct type Fix #14601 --- test/types/subdocuments.test.ts | 43 ++++++++++++++++++++++++++++++++- types/index.d.ts | 4 +-- types/types.d.ts | 2 +- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/test/types/subdocuments.test.ts b/test/types/subdocuments.test.ts index 59b58716f5f..14386816fe5 100644 --- a/test/types/subdocuments.test.ts +++ b/test/types/subdocuments.test.ts @@ -1,4 +1,13 @@ -import { Schema, model, Model, Document, Types } from 'mongoose'; +import { + Schema, + model, + Model, + Document, + Types, + HydratedArraySubdocument, + HydratedSingleSubdocument +} from 'mongoose'; +import { expectAssignable } from 'tsd'; const childSchema: Schema = new Schema({ name: String }); @@ -108,3 +117,35 @@ function gh13040(): void { product.ownerDocument(); }); } + +function gh14601() { + interface ISub { + field1: string; + } + interface IMain { + f1: string; + f2: HydratedSingleSubdocument; + f3: HydratedArraySubdocument[]; + } + + const subSchema = new Schema({ field1: String }, { _id: false }); + + const mainSchema = new Schema({ + f1: String, + f2: { type: subSchema }, + f3: { type: [subSchema] } + }); + const MainModel = model('Main', mainSchema); + + const item = new MainModel({ + f1: 'test', + f2: { field1: 'test' }, + f3: [{ field1: 'test' }] + }); + + const obj = item.toObject(); + + const obj2 = item.f2.toObject(); + + expectAssignable<{ _id: Types.ObjectId, field1: string }>(obj2); +} diff --git a/types/index.d.ts b/types/index.d.ts index 2e2a5fbc6a7..20177d9d37d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -157,8 +157,8 @@ declare module 'mongoose' { > > >; - export type HydratedSingleSubdocument = Types.Subdocument & Require_id & TOverrides; - export type HydratedArraySubdocument = Types.ArraySubdocument & Require_id & TOverrides; + export type HydratedSingleSubdocument = Types.Subdocument, DocType> & Require_id & TOverrides; + export type HydratedArraySubdocument = Types.ArraySubdocument, DocType> & Require_id & TOverrides; export type HydratedDocumentFromSchema = HydratedDocument< InferSchemaType, diff --git a/types/types.d.ts b/types/types.d.ts index 08f90c6184c..194917d69ec 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -96,7 +96,7 @@ declare module 'mongoose' { $parent(): Document; } - class ArraySubdocument extends Subdocument { + class ArraySubdocument extends Subdocument { /** Returns this sub-documents parent array. */ parentArray(): Types.DocumentArray; } From 2290c818ab86e9457ad0475902a078f7bcae317d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 30 May 2024 07:41:29 -0400 Subject: [PATCH 2/2] Update subdocuments.test.ts --- test/types/subdocuments.test.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/types/subdocuments.test.ts b/test/types/subdocuments.test.ts index 14386816fe5..82204d0d293 100644 --- a/test/types/subdocuments.test.ts +++ b/test/types/subdocuments.test.ts @@ -143,9 +143,6 @@ function gh14601() { f3: [{ field1: 'test' }] }); - const obj = item.toObject(); - - const obj2 = item.f2.toObject(); - - expectAssignable<{ _id: Types.ObjectId, field1: string }>(obj2); + const f2obj = item.f2.toObject(); + expectAssignable<{ _id: Types.ObjectId, field1: string }>(f2obj); }