Skip to content

Commit

Permalink
fix(types): correct return value for Model.exists()
Browse files Browse the repository at this point in the history
Fix #12094
  • Loading branch information
vkarpov15 committed Jul 25, 2022
1 parent e2db583 commit edcf468
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 13 additions & 0 deletions test/types/models.test.ts
Expand Up @@ -345,3 +345,16 @@ function gh12100() {

expectType<string>(obj._id);
})();

(async function gh12094() {
const userSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});

const User = model('User', userSchema);

const doc = await User.exists({ name: 'Bill' }).orFail();
expectType<Types.ObjectId>(doc._id);
})();
10 changes: 5 additions & 5 deletions types/models.d.ts
Expand Up @@ -306,11 +306,11 @@ declare module 'mongoose' {
estimatedDocumentCount(options?: QueryOptions<T>, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;

/**
* Returns a document with its `_id` if at least one document exists in the database that matches
* the given `filter`, and `null` otherwise.
*/
exists(filter: FilterQuery<T>, callback: Callback<Pick<Document<T>, '_id'> | null>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
exists(filter: FilterQuery<T>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
* Returns a document with its `_id` if at least one document exists in the database that matches
* the given `filter`, and `null` otherwise.
*/
exists(filter: FilterQuery<T>, callback: Callback<{ _id: InferId<T> } | null>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
exists(filter: FilterQuery<T>): QueryWithHelpers<{ _id: InferId<T> } | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;

/** Creates a `find` query: gets a list of documents that match `filter`. */
find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
Expand Down

0 comments on commit edcf468

Please sign in to comment.