From 456793eddf33bf579f63aaa9690576f17641b42b Mon Sep 17 00:00:00 2001 From: dmshvetsov Date: Tue, 3 Jan 2023 19:52:37 +0800 Subject: [PATCH 1/2] add type def for Aggreaget#model without arguments --- types/aggregate.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/types/aggregate.d.ts b/types/aggregate.d.ts index 7264a2f7db1..926e878fcdd 100644 --- a/types/aggregate.d.ts +++ b/types/aggregate.d.ts @@ -166,6 +166,11 @@ declare module 'mongoose' { */ model(model: Model): this; + /** + * Returns the current model bound to this aggregate object + */ + model(): Model; + /** * Append a new $near operator to this aggregation pipeline * @param arg $near operator contents From 5faf524ade976e4422af9573bb475e983cd00b4a Mon Sep 17 00:00:00 2001 From: dmshvetsov Date: Wed, 4 Jan 2023 20:01:39 +0800 Subject: [PATCH 2/2] add type tests for Aggregate.prototype.model --- test/types/aggregate.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/types/aggregate.test.ts b/test/types/aggregate.test.ts index 6284cd649fd..8d0d0bd16b3 100644 --- a/test/types/aggregate.test.ts +++ b/test/types/aggregate.test.ts @@ -1,4 +1,4 @@ -import { Schema, model, Document, Expression, PipelineStage, Types } from 'mongoose'; +import { Schema, model, Document, Expression, PipelineStage, Types, Model, Aggregate } from 'mongoose'; import { expectType } from 'tsd'; const schema: Schema = new Schema({ name: { type: 'String' } }); @@ -8,6 +8,7 @@ interface ITest extends Document { } const Test = model('Test', schema); +const AnotherTest = model('AnotherTest', schema); Test.aggregate([{ $match: { name: 'foo' } }]).exec().then((res: any) => console.log(res)); @@ -60,6 +61,10 @@ async function run() { expectType(await Test.aggregate().sort({ name: 'desc' })); expectType(await Test.aggregate().sort({ name: 'descending' })); expectType(await Test.aggregate().sort({ name: { $meta: 'textScore' } })); + + // Aggregate.prototype.model() + expectType>(Test.aggregate().model()); + expectType>(Test.aggregate().model(AnotherTest)); } function gh12017_1() {