Skip to content

Commit

Permalink
fix(types): more flexible expressions for aggregations
Browse files Browse the repository at this point in the history
Re: #12017
  • Loading branch information
vkarpov15 committed Jul 3, 2022
1 parent a55d388 commit 9f3f8eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
40 changes: 39 additions & 1 deletion test/types/aggregate.test.ts
@@ -1,4 +1,4 @@
import { Schema, model, Document, Types } from 'mongoose';
import { Schema, model, Document, Expression, PipelineStage, Types } from 'mongoose';
import { expectType } from 'tsd';

const schema: Schema = new Schema({ name: { type: 'String' } });
Expand Down Expand Up @@ -61,3 +61,41 @@ async function run() {
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'descending' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: { $meta: 'textScore' } }));
}

function gh12017_1() {
const a: Expression = { $subtract: [
{ $dayOfWeek: new Date() } as Expression.DayOfWeek, // errors
2
] };
}

function gh12017_2() {
const a: Expression.Reduce = {
$reduce: {
input: '$values',
initialValue: { depth: -1 }, // errors
in: {
depth: '$$this.depth' // errors
}
}
};

const b: Expression.Reduce = {
$reduce: {
input: '$values',
initialValue: 0,
in: { $add: ['$$value', '$$this'] }
}
};

const c: PipelineStage.Set = {
$set: {
child: {
foo: 'bar' // errors
},
friend: new Types.ObjectId()
}
};

const d: Expression.ToInt = { $toInt: 2.5 };
}
10 changes: 6 additions & 4 deletions types/expressions.d.ts
Expand Up @@ -1043,7 +1043,7 @@ declare module 'mongoose' {
*
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/cond/#mongodb-expression-exp.-cond
*/
$cond: { if: BooleanExpression, then: AnyExpression, else: AnyExpression } | [BooleanExpression, AnyExpression, AnyExpression];
$cond: { if: BooleanExpression | ConditionalExpressionOperator, then: AnyExpression, else: AnyExpression } | [BooleanExpression, AnyExpression, AnyExpression];
}

export interface IfNull {
Expand Down Expand Up @@ -1957,7 +1957,7 @@ declare module 'mongoose' {
* @version 5.0
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/addToSet/#mongodb-expression-exp.-addToSet
*/
$addToSet: ArrayExpression;
$addToSet: Expression | Record<string, Expression>;
}

export interface Avg {
Expand Down Expand Up @@ -2406,7 +2406,8 @@ declare module 'mongoose' {
TypeExpressionOperator |
AccumulatorOperator |
VariableExpressionOperator |
WindowOperator;
WindowOperator |
any;

export type NullExpression = null;

Expand Down Expand Up @@ -2478,7 +2479,8 @@ declare module 'mongoose' {
DataSizeOperatorReturningNumber |
CustomAggregationExpressionOperatorReturningAny |
TypeExpressionOperatorReturningNumber |
DateExpression;
DateExpression |
DateExpressionOperatorReturningNumber;

export type ObjectExpression =
Path |
Expand Down
2 changes: 1 addition & 1 deletion types/pipelinestage.d.ts
Expand Up @@ -212,7 +212,7 @@ declare module 'mongoose' {

export interface Set {
/** [`$set` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/set/) */
$set: Record<string, AnyExpression>
$set: Record<string, AnyExpression | any>
}

export interface SetWindowFields {
Expand Down

0 comments on commit 9f3f8eb

Please sign in to comment.