diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index 1970e11bfd3..5c019e3375b 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -339,3 +339,33 @@ function gh12136() { } } + +async function gh13070() { + interface IParent { + name: string; + child: Types.ObjectId; + } + interface IChild { + name: string; + parent: Types.ObjectId; + } + + const parentSchema = new Schema( + { + name: { type: String, required: true }, + child: { type: Schema.Types.ObjectId, ref: 'Child', required: true } + }); + + const childSchema = new Schema( + { + name: { type: String, required: true }, + parent: { type: Schema.Types.ObjectId, ref: 'Parent', required: true } + }); + + const Parent = model('Parent', parentSchema); + const Child = model('Child', childSchema); + + const doc = await Parent.findOne().orFail(); + const doc2 = await Child.populate<{ child: IChild }>(doc, 'child'); + const name: string = doc2.child.name; +} diff --git a/types/models.d.ts b/types/models.d.ts index 46f416a7570..5a557d21d36 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -299,11 +299,22 @@ declare module 'mongoose' { modelName: string; /** Populates document references. */ - populate(docs: Array, options: PopulateOptions | Array | string, - callback?: Callback<(HydratedDocument)[]>): Promise>>; - populate(doc: any, options: PopulateOptions | Array | string, - callback?: Callback>): Promise>; - + populate( + docs: Array, + options: PopulateOptions | Array | string + ): Promise>>; + populate( + doc: any, + options: PopulateOptions | Array | string, + ): Promise>; + populate( + docs: Array, + options: PopulateOptions | Array | string + ): Promise, Paths>[]>; + populate( + doc: AnyObject, + options: PopulateOptions | Array | string + ): Promise, Paths>>; /** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */ validate(callback?: CallbackWithoutResult): Promise;