Skip to content

Commit

Permalink
Merge pull request #14196 from Automattic/vkarpov15/gh-14190
Browse files Browse the repository at this point in the history
Fix return types for findByIdAndDelete overrides
  • Loading branch information
vkarpov15 committed Dec 27, 2023
2 parents 3bd67a8 + 2c3697f commit 4b069e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
26 changes: 25 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 @@ -520,3 +520,27 @@ function gh13630() {
const x: UpdateQueryKnownOnly<User> = { $set: { name: 'John' } };
expectAssignable<UpdateQuery<User>>(x);
}

function gh14190() {
const userSchema = new Schema({ name: String, age: Number });
const UserModel = model('User', userSchema);

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: 2 additions & 2 deletions types/models.d.ts
Expand Up @@ -564,8 +564,8 @@ declare module 'mongoose' {
'findOneAndDelete'
>;
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
id?: mongodb.ObjectId | any,
options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
id: mongodb.ObjectId | any,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
id?: mongodb.ObjectId | any,
Expand Down
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 4b069e8

Please sign in to comment.