Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(document): correct return type for doc.deleteOne() re: Mongoose 8 breaking change #14110

Merged
merged 1 commit into from Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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