diff --git a/test/types/aggregate.test.ts b/test/types/aggregate.test.ts index b227a4099fd..0cf72c11339 100644 --- a/test/types/aggregate.test.ts +++ b/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' } }); @@ -61,3 +61,41 @@ async function run() { expectType(await Test.aggregate().sort({ name: 'descending' })); expectType(await Test.aggregate().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 }; +} diff --git a/types/expressions.d.ts b/types/expressions.d.ts index 8663e1a49fb..be5b5789741 100644 --- a/types/expressions.d.ts +++ b/types/expressions.d.ts @@ -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 { @@ -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; } export interface Avg { @@ -2406,7 +2406,8 @@ declare module 'mongoose' { TypeExpressionOperator | AccumulatorOperator | VariableExpressionOperator | - WindowOperator; + WindowOperator | + any; export type NullExpression = null; @@ -2478,7 +2479,8 @@ declare module 'mongoose' { DataSizeOperatorReturningNumber | CustomAggregationExpressionOperatorReturningAny | TypeExpressionOperatorReturningNumber | - DateExpression; + DateExpression | + DateExpressionOperatorReturningNumber; export type ObjectExpression = Path | diff --git a/types/pipelinestage.d.ts b/types/pipelinestage.d.ts index 4972d265de3..a9dfb4659e8 100644 --- a/types/pipelinestage.d.ts +++ b/types/pipelinestage.d.ts @@ -212,7 +212,7 @@ declare module 'mongoose' { export interface Set { /** [`$set` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/set/) */ - $set: Record + $set: Record } export interface SetWindowFields {