Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): aggregate.sort() accepts a string but also { field: 'asc'|'ascending'|'desc'|'descending' } #11479

Merged
merged 3 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/types/aggregate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Schema, model, Document, Types } from 'mongoose';
import { expectType } from 'tsd';

const schema: Schema = new Schema({ name: { type: 'String' } });

Expand Down Expand Up @@ -30,4 +31,14 @@ async function run() {
for await (const obj of Test.aggregate<ITest>()) {
obj.name;
}

// Aggregate.prototype.sort()
expectType<ITest[]>(await Test.aggregate<ITest>().sort('-name'));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 1 }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: -1 }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'asc' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'ascending' }));
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' } }));
}
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2500,7 +2500,7 @@ declare module 'mongoose' {
$each: Type;
};

type SortValues = -1 | 1 | 'asc' | 'desc';
type SortValues = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';

type ArrayOperator<Type> = {
$each: Type;
Expand Down Expand Up @@ -2790,7 +2790,7 @@ declare module 'mongoose' {
skip(num: number): this;

/** Appends a new $sort operator to this aggregate pipeline. */
sort(arg: PipelineStage.Sort['$sort']): this;
sort(arg: string | Record<string, SortValues> | PipelineStage.Sort['$sort']): this;

/** Provides promise for aggregate. */
then: Promise<R>['then'];
Expand Down