diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 8d022594753..39ffb7ce6f2 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -345,3 +345,16 @@ function gh12100() { expectType(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(doc._id); +})(); diff --git a/types/models.d.ts b/types/models.d.ts index d4a6f1a1797..0dc9c6aa95f 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -306,11 +306,11 @@ declare module 'mongoose' { estimatedDocumentCount(options?: QueryOptions, callback?: Callback): QueryWithHelpers, 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, callback: Callback, '_id'> | null>): QueryWithHelpers, '_id'> | null, HydratedDocument, TQueryHelpers, T>; - exists(filter: FilterQuery): QueryWithHelpers, '_id'> | null, HydratedDocument, 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, callback: Callback<{ _id: InferId } | null>): QueryWithHelpers, '_id'> | null, HydratedDocument, TQueryHelpers, T>; + exists(filter: FilterQuery): QueryWithHelpers<{ _id: InferId } | null, HydratedDocument, TQueryHelpers, T>; /** Creates a `find` query: gets a list of documents that match `filter`. */ find>(