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 Dec 26, 2023
1 parent 7d1f4e4 commit 0c7c9bd
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 @@ -527,4 +527,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);

Check warning on line 537 in test/types/queries.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 2

Check failure on line 537 in test/types/queries.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 2

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

Check warning on line 545 in test/types/queries.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 2

Check failure on line 545 in test/types/queries.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 2
}
4 changes: 4 additions & 0 deletions types/query.d.ts
Expand Up @@ -415,6 +415,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 0c7c9bd

Please sign in to comment.