Skip to content

Commit

Permalink
types(query): improve findByIdAndDelete return type for query re: #14190
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 21, 2024
1 parent 775aadf commit 7f9406f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 17 additions & 1 deletion test/types/queries.test.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -525,4 +525,20 @@ function gh14190() {

const doc = await UserModel.findByIdAndDelete('0'.repeat(24));
expectType<ReturnType<(typeof UserModel)['hydrate']> | null>(doc);

const res = await UserModel.findByIdAndDelete(
'0'.repeat(24),
{ includeResultMetadata: true }
);
expectAssignable<
ModifyResult<ReturnType<(typeof UserModel)['hydrate']>>
>(res);

const res2 = await UserModel.find().findByIdAndDelete(
'0'.repeat(24),
{ includeResultMetadata: true }
);
expectAssignable<
ModifyResult<ReturnType<(typeof UserModel)['hydrate']>>
>(res2);
}
4 changes: 4 additions & 0 deletions types/query.d.ts
Expand Up @@ -435,6 +435,10 @@ declare module 'mongoose' {
): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne'>;

/** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
findByIdAndDelete(
id: mongodb.ObjectId | any,
options: QueryOptions<DocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType, 'findOneAndDelete'>;
findByIdAndDelete(
id?: mongodb.ObjectId | any,
options?: QueryOptions<DocType> | null
Expand Down

0 comments on commit 7f9406f

Please sign in to comment.