From 34a55882ef8755ec0ccefdc04abbab0eeed4a8e9 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Thu, 2 Mar 2023 14:37:44 -0500 Subject: [PATCH 1/4] Add Path genereic It wasn't yelling at me --- test/types/populate.test.ts | 31 +++++++++++++++++++++++++++++++ types/models.d.ts | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index 1970e11bfd3..feb5e049a42 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -339,3 +339,34 @@ 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(); + await child.populate<{child: IChild}>(doc, 'child'); + + +} diff --git a/types/models.d.ts b/types/models.d.ts index 46f416a7570..51d078e1683 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -303,7 +303,8 @@ declare module 'mongoose' { callback?: Callback<(HydratedDocument)[]>): Promise>>; populate(doc: any, options: PopulateOptions | Array | string, callback?: Callback>): Promise>; - + populate(docs: Array, options: PopulateOptions | Array | string): Promise>; + populate(docs: any, options: PopulateOptions | Array | string, callback?: Callback>): void; /** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */ validate(callback?: CallbackWithoutResult): Promise; From fd367cc11fb20a6ad2563b25bdcd9ecfa05845fb Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Thu, 2 Mar 2023 14:41:35 -0500 Subject: [PATCH 2/4] fix: lint --- test/types/populate.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index feb5e049a42..5ef8c57ef53 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -366,7 +366,7 @@ async function gh13070() { const child = model('Child', childSchema); const doc = await parent.findOne(); - await child.populate<{child: IChild}>(doc, 'child'); + await child.populate<{ child: IChild }>(doc, 'child'); } From b7417ba3389792b7b2ccaa89aa22274890693e60 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 2 Mar 2023 15:56:41 -0500 Subject: [PATCH 3/4] Update populate.test.ts --- test/types/populate.test.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index 5ef8c57ef53..d65d2cb35ad 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -362,11 +362,10 @@ async function gh13070() { parent: { type: Schema.Types.ObjectId, ref: 'Parent', required: true } }); - const parent = model('Parent', parentSchema); - const child = model('Child', childSchema); - - const doc = await parent.findOne(); - await child.populate<{ child: IChild }>(doc, 'child'); - + const Parent = model('Parent', parentSchema); + const Child = model('Child', childSchema); + const doc = await Parent.findOne(); + const doc2 = await Child.populate<{ child: IChild }>(doc, 'child'); + const name: string = doc.child.name; } From 66d02350553a754122832e8ee903556e1c5f7398 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 2 Mar 2023 16:11:07 -0500 Subject: [PATCH 4/4] fix tests, fix #13070 --- test/types/populate.test.ts | 4 ++-- types/models.d.ts | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index d65d2cb35ad..5c019e3375b 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -365,7 +365,7 @@ async function gh13070() { const Parent = model('Parent', parentSchema); const Child = model('Child', childSchema); - const doc = await Parent.findOne(); + const doc = await Parent.findOne().orFail(); const doc2 = await Child.populate<{ child: IChild }>(doc, 'child'); - const name: string = doc.child.name; + const name: string = doc2.child.name; } diff --git a/types/models.d.ts b/types/models.d.ts index 51d078e1683..5a557d21d36 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -299,12 +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(docs: any, options: PopulateOptions | Array | string, callback?: Callback>): void; + 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;