diff --git a/test/types/subdocuments.test.ts b/test/types/subdocuments.test.ts index 59b58716f5f..82204d0d293 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,32 @@ 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 f2obj = item.f2.toObject(); + expectAssignable<{ _id: Types.ObjectId, field1: string }>(f2obj); +} 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; }