Skip to content

Commit

Permalink
Merge pull request #12864 from dmshvetsov/dmshvetsov/fix-type-def-agg…
Browse files Browse the repository at this point in the history
…regation-model-fn

add type def for Aggreaget#model without arguments
  • Loading branch information
AbdelrahmanHafez committed Jan 4, 2023
2 parents d10ad8c + 5faf524 commit e9b5eed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion 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' } });
Expand All @@ -8,6 +8,7 @@ interface ITest extends Document {
}

const Test = model<ITest>('Test', schema);
const AnotherTest = model<ITest>('AnotherTest', schema);

Test.aggregate([{ $match: { name: 'foo' } }]).exec().then((res: any) => console.log(res));

Expand Down Expand Up @@ -60,6 +61,10 @@ async function run() {
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'desc' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'descending' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: { $meta: 'textScore' } }));

// Aggregate.prototype.model()
expectType<Model<any>>(Test.aggregate<ITest>().model());
expectType<Aggregate<ITest[]>>(Test.aggregate<ITest>().model(AnotherTest));
}

function gh12017_1() {
Expand Down
5 changes: 5 additions & 0 deletions types/aggregate.d.ts
Expand Up @@ -166,6 +166,11 @@ declare module 'mongoose' {
*/
model(model: Model<any>): this;

/**
* Returns the current model bound to this aggregate object
*/
model(): Model<any>;

/**
* Append a new $near operator to this aggregation pipeline
* @param arg $near operator contents
Expand Down

0 comments on commit e9b5eed

Please sign in to comment.