Skip to content

Commit

Permalink
Merge pull request #14110 from Automattic/vkarpov15/gh-14081
Browse files Browse the repository at this point in the history
types(document): correct return type for `doc.deleteOne()` re: Mongoose 8 breaking change
  • Loading branch information
vkarpov15 committed Nov 22, 2023
2 parents d53b1c5 + c424f10 commit 3e0a90c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion test/types/document.test.ts
Expand Up @@ -9,6 +9,7 @@ import {
HydratedSingleSubdocument,
DefaultSchemaOptions
} from 'mongoose';
import { DeleteResult } from 'mongodb';
import { expectAssignable, expectError, expectType } from 'tsd';
import { autoTypedModel } from './models.test';
import { autoTypedModelConnection } from './connection.test';
Expand Down Expand Up @@ -39,7 +40,9 @@ const Test = model<ITest>('Test', schema);
void async function main() {
const doc = await Test.findOne().orFail();

expectType<Promise<TestDocument>>(doc.deleteOne());
expectType<DeleteResult>(await doc.deleteOne());
expectType<TestDocument | null>(await doc.deleteOne().findOne());
expectType<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean());
}();


Expand Down
8 changes: 7 additions & 1 deletion types/document.d.ts
Expand Up @@ -108,7 +108,13 @@ declare module 'mongoose' {
db: Connection;

/** Removes this document from the db. */
deleteOne(options?: QueryOptions): Promise<this>;
deleteOne(options?: QueryOptions): QueryWithHelpers<
mongodb.DeleteResult,
this,
TQueryHelpers,
DocType,
'deleteOne'
>;

/**
* Takes a populated field and returns it to its unpopulated state. If called with
Expand Down

0 comments on commit 3e0a90c

Please sign in to comment.