diff --git a/test/types/document.test.ts b/test/types/document.test.ts index d492cebd6e7..84451edf0f2 100644 --- a/test/types/document.test.ts +++ b/test/types/document.test.ts @@ -10,7 +10,7 @@ import { DefaultSchemaOptions } from 'mongoose'; import { DeleteResult } from 'mongodb'; -import { expectAssignable, expectError, expectType } from 'tsd'; +import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'; import { autoTypedModel } from './models.test'; import { autoTypedModelConnection } from './connection.test'; import { AutoTypedSchemaType } from './schema.test'; @@ -42,7 +42,8 @@ void async function main() { expectType(await doc.deleteOne()); expectType(await doc.deleteOne().findOne()); - expectType<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean()); + expectAssignable<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean()); + expectNotAssignable(await doc.deleteOne().findOne().lean()); }(); diff --git a/test/types/plugin.test.ts b/test/types/plugin.test.ts index 0b579ccc6b9..fc1deddf900 100644 --- a/test/types/plugin.test.ts +++ b/test/types/plugin.test.ts @@ -49,7 +49,7 @@ interface TestStaticMethods { findSomething(this: TestModel): Promise; } type TestDocument = HydratedDocument; -type TestQuery = Query & TestQueryHelpers; +type TestQuery = Query & TestQueryHelpers; interface TestQueryHelpers { whereSomething(this: TestQuery): this } diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index f10f26b3ad2..d84022526e4 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -38,11 +38,11 @@ const schema: Schema, {}, QueryHelpers> = new endDate: Date }); -schema.query._byName = function(name: string): QueryWithHelpers { +schema.query._byName = function(name: string): QueryWithHelpers { return this.find({ name }); }; -schema.query.byName = function(name: string): QueryWithHelpers { +schema.query.byName = function(name: string): QueryWithHelpers { expectError(this.notAQueryHelper()); return this._byName(name); }; diff --git a/types/query.d.ts b/types/query.d.ts index 44d9a71062f..850d06eb25a 100644 --- a/types/query.d.ts +++ b/types/query.d.ts @@ -224,7 +224,7 @@ declare module 'mongoose' { : MergeType : MergeType; - class Query> implements SessionOperation { + class Query> implements SessionOperation { _mongooseOptions: MongooseQueryOptions; /** @@ -548,9 +548,19 @@ declare module 'mongoose' { j(val: boolean | null): this; /** Sets the lean option. */ - lean< - LeanResultType = GetLeanResultType - >( + lean( + val?: boolean | any + ): QueryWithHelpers< + ResultType extends null + ? GetLeanResultType | null + : GetLeanResultType, + DocType, + THelpers, + RawDocType, + QueryOp, + TInstanceMethods + >; + lean( val?: boolean | any ): QueryWithHelpers< ResultType extends null