Skip to content

Commit 6d91d94

Browse files
authored
Merge pull request #11924 from GCastilho/fix/post-hook-missing-t
fix: post hook 'res' param with type any
2 parents 64dbbb3 + 9f6edfe commit 6d91d94

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

test/types/middleware.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ schema.post<Aggregate<any>>('aggregate', async function(res: Array<any>) {
3232
console.log('Pipeline', this.pipeline(), res[0]);
3333
});
3434

35+
schema.post<Aggregate<ITest>>('aggregate', function(res, next) {
36+
expectType<ITest[]>(res);
37+
next();
38+
});
39+
40+
schema.post<Query<ITest, ITest>>('save', function(res, next) {
41+
expectType<Query<ITest, ITest>>(res);
42+
next();
43+
});
44+
3545
schema.pre(['save', 'validate'], { query: false, document: true }, async function applyChanges() {
3646
await Test.findOne({});
3747
});
@@ -45,6 +55,11 @@ schema.pre('save', function(next) {
4555
console.log(this.name);
4656
});
4757

58+
schema.post<ITest>('save', function(res, next) {
59+
expectType<ITest>(res);
60+
next();
61+
});
62+
4863
schema.post<ITest>('save', function() {
4964
console.log(this.name);
5065
});
@@ -96,4 +111,4 @@ function gh11480(): void {
96111
expectNotType<any>(this);
97112
next();
98113
});
99-
}
114+
}

types/aggregate.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
declare module 'mongoose' {
22
import mongodb = require('mongodb');
33

4+
/** Extract generic type from Aggregate class */
5+
type AggregateExtract<P> = P extends Aggregate<infer T> ? T : never;
6+
47
interface AggregateOptions extends
58
SessionOption {
69
/**

types/index.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ declare module 'mongoose' {
211211
plugin(fn: (schema: Schema<DocType>, opts?: any) => void, opts?: any): this;
212212

213213
/** Defines a post hook for the model. */
214-
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T>): this;
215-
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T>): this;
216-
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, fn: PostMiddlewareFunction<T>): this;
217-
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T>): this;
218-
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction<T, Array<any>>): this;
219-
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<any>>): this;
220-
post<T = M>(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<T>): this;
221-
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T>): this;
214+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;
215+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
216+
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, fn: PostMiddlewareFunction<T, T>): this;
217+
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
218+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
219+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
220+
post<T = M>(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<T, T>): this;
221+
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
222222

223223
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
224224
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;

0 commit comments

Comments
 (0)