Skip to content

Commit 3e0a90c

Browse files
authored
Merge pull request #14110 from Automattic/vkarpov15/gh-14081
types(document): correct return type for `doc.deleteOne()` re: Mongoose 8 breaking change
2 parents d53b1c5 + c424f10 commit 3e0a90c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

test/types/document.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
HydratedSingleSubdocument,
1010
DefaultSchemaOptions
1111
} from 'mongoose';
12+
import { DeleteResult } from 'mongodb';
1213
import { expectAssignable, expectError, expectType } from 'tsd';
1314
import { autoTypedModel } from './models.test';
1415
import { autoTypedModelConnection } from './connection.test';
@@ -39,7 +40,9 @@ const Test = model<ITest>('Test', schema);
3940
void async function main() {
4041
const doc = await Test.findOne().orFail();
4142

42-
expectType<Promise<TestDocument>>(doc.deleteOne());
43+
expectType<DeleteResult>(await doc.deleteOne());
44+
expectType<TestDocument | null>(await doc.deleteOne().findOne());
45+
expectType<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean());
4346
}();
4447

4548

types/document.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ declare module 'mongoose' {
108108
db: Connection;
109109

110110
/** Removes this document from the db. */
111-
deleteOne(options?: QueryOptions): Promise<this>;
111+
deleteOne(options?: QueryOptions): QueryWithHelpers<
112+
mongodb.DeleteResult,
113+
this,
114+
TQueryHelpers,
115+
DocType,
116+
'deleteOne'
117+
>;
112118

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

0 commit comments

Comments
 (0)