diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 02a41836bab..efdce8ff9f3 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -17,7 +17,7 @@ import { ProjectionFields, QueryOptions } from 'mongoose'; -import { ObjectId } from 'mongodb'; +import { ModifyResult, ObjectId } from 'mongodb'; import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'; import { autoTypedModel } from './models.test'; import { AutoTypedSchemaType } from './schema.test'; @@ -520,3 +520,27 @@ function gh13630() { const x: UpdateQueryKnownOnly = { $set: { name: 'John' } }; expectAssignable>(x); } + +function gh14190() { + const userSchema = new Schema({ name: String, age: Number }); + const UserModel = model('User', userSchema); + + const doc = await UserModel.findByIdAndDelete('0'.repeat(24)); + expectType | null>(doc); + + const res = await UserModel.findByIdAndDelete( + '0'.repeat(24), + { includeResultMetadata: true } + ); + expectAssignable< + ModifyResult> + >(res); + + const res2 = await UserModel.find().findByIdAndDelete( + '0'.repeat(24), + { includeResultMetadata: true } + ); + expectAssignable< + ModifyResult> + >(res2); +} diff --git a/types/models.d.ts b/types/models.d.ts index 3f79fd5b4d3..ebd7f80f13a 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -564,8 +564,8 @@ declare module 'mongoose' { 'findOneAndDelete' >; findByIdAndDelete( - id?: mongodb.ObjectId | any, - options?: QueryOptions & { includeResultMetadata: true } + id: mongodb.ObjectId | any, + options: QueryOptions & { includeResultMetadata: true } ): QueryWithHelpers, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>; findByIdAndDelete( id?: mongodb.ObjectId | any, diff --git a/types/query.d.ts b/types/query.d.ts index ed867cb8bde..a7c4ac22db5 100644 --- a/types/query.d.ts +++ b/types/query.d.ts @@ -415,6 +415,10 @@ declare module 'mongoose' { ): QueryWithHelpers; /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */ + findByIdAndDelete( + id: mongodb.ObjectId | any, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers, DocType, THelpers, RawDocType, 'findOneAndDelete'>; findByIdAndDelete( id?: mongodb.ObjectId | any, options?: QueryOptions | null