diff --git a/test/types/docArray.test.ts b/test/types/docArray.test.ts index cf41df0b82e..c296ce6fea7 100644 --- a/test/types/docArray.test.ts +++ b/test/types/docArray.test.ts @@ -1,4 +1,4 @@ -import { Schema, model, Types, InferSchemaType } from 'mongoose'; +import { Schema, model, Model, Types, InferSchemaType } from 'mongoose'; import { expectError, expectType } from 'tsd'; async function gh10293() { @@ -127,3 +127,38 @@ async function gh14367() { expectType({} as IUser['reminders'][0]['toggle']); expectType({} as IUser['avatar']); } + +function gh14469() { + interface Names { + _id: Types.ObjectId; + firstName: string; + } + // Document definition + interface User { + names: Names[]; + } + + // TMethodsAndOverrides + type UserDocumentProps = { + names: Types.DocumentArray; + }; + type UserModelType = Model; + + const userSchema = new Schema( + { + names: [new Schema({ firstName: String })] + }, + { timestamps: true } + ); + + // Create model + const UserModel = model('User', userSchema); + + const doc = new UserModel({ names: [{ firstName: 'John' }] }); + + const jsonDoc = doc?.toJSON(); + expectType(jsonDoc?.names[0]?.firstName); + + const jsonNames = doc?.names[0]?.toJSON(); + expectType(jsonNames?.firstName); +} diff --git a/types/types.d.ts b/types/types.d.ts index cce6b12504c..f63b1934907 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -60,7 +60,7 @@ declare module 'mongoose' { class Decimal128 extends mongodb.Decimal128 { } - class DocumentArray extends Types.Array> & T> { + class DocumentArray extends Types.Array, any, T> & T> { /** DocumentArray constructor */ constructor(values: AnyObject[]); @@ -83,7 +83,7 @@ declare module 'mongoose' { class ObjectId extends mongodb.ObjectId { } - class Subdocument extends Document { + class Subdocument extends Document { $isSingleNested: true; /** Returns the top level document of this sub-document. */