Skip to content

Commit

Permalink
Merge pull request #13632 from Automattic/vkarpov15/gh-13601
Browse files Browse the repository at this point in the history
types: correctly handle `pre('deleteOne', { document: true })`
  • Loading branch information
vkarpov15 committed Jul 20, 2023
2 parents ef95e02 + 690fa2d commit 49fa3ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion test/types/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction } from 'mongoose';
import { expectError, expectType, expectNotType } from 'tsd';
import { expectError, expectType, expectNotType, expectAssignable } from 'tsd';

const preMiddlewareFn: PreSaveMiddlewareFunction<Document> = function(next, opts) {
this.$markValid('name');
Expand Down Expand Up @@ -173,3 +173,13 @@ function gh11257() {
this.find();
});
}

function gh13601() {
const testSchema = new Schema({
name: String
});

testSchema.pre('deleteOne', { document: true }, function() {
expectAssignable<Document>(this);
});
}
11 changes: 11 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,22 @@ declare module 'mongoose' {
pre<T = never>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: false }, fn: PreMiddlewareFunction<T>): this;
pre<T = never>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPreOptions & { document: boolean, query: false }, fn: PreMiddlewareFunction<T>): this;
pre<T = never>(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: boolean }, fn: PreMiddlewareFunction<T>): this;
// this = Union of Document and Query, could be called with any of them
pre<T = THydratedDocumentType | Query<any, any>>(
method: MongooseQueryAndDocumentMiddleware | MongooseQueryAndDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: true, query: true },
fn: PreMiddlewareFunction<T>
): this;
// this = Document
pre<T = THydratedDocumentType>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PreMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(
method: MongooseQueryAndDocumentMiddleware | MongooseQueryAndDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: true },
fn: PreMiddlewareFunction<T>
): this;
pre<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: true, query: false }, fn: PreMiddlewareFunction<T>): this;
// this = Query
pre<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PreMiddlewareFunction<T>): this;
Expand Down

0 comments on commit 49fa3ee

Please sign in to comment.