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

Fix return types for findByIdAndDelete overrides #14196

Merged
merged 3 commits into from Dec 27, 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
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