From 7d1f4e44d1727729956bdc74a5f7fcfed825c4c4 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 26 Dec 2023 15:53:14 -0500 Subject: [PATCH 1/3] types(model): correct return type for findByIdAndDelete() Fix #14190 --- test/types/queries.test.ts | 8 ++++++++ types/models.d.ts | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 02a41836bab..792659c4a1f 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -520,3 +520,11 @@ 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); +} 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, From 0c7c9bd0322631cac0a2a3f3b7b241aff735cd7d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 26 Dec 2023 16:14:07 -0500 Subject: [PATCH 2/3] types(query): improve findByIdAndDelete return type for query re: #14190 --- test/types/queries.test.ts | 18 +++++++++++++++++- types/query.d.ts | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 792659c4a1f..3e966cdbfe5 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'; @@ -527,4 +527,20 @@ function gh14190() { 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/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 From 2c3697f8a34a00c6e5a1051f7b32a7e446d73854 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 27 Dec 2023 14:24:55 -0500 Subject: [PATCH 3/3] style: fix lint --- test/types/queries.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 3e966cdbfe5..efdce8ff9f3 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -534,7 +534,7 @@ function gh14190() { ); expectAssignable< ModifyResult> - >(res); + >(res); const res2 = await UserModel.find().findByIdAndDelete( '0'.repeat(24), @@ -542,5 +542,5 @@ function gh14190() { ); expectAssignable< ModifyResult> - >(res2); + >(res2); }