Skip to content

Commit 9f6edfe

Browse files
committed
Add tests for type changes
-- Add Utility Type do extract type from Aggregate class -- Fix ResType type for aggregate hook -- Add tests for post 'save' and post 'aggregate' hooks
1 parent 85fc262 commit 9f6edfe

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ declare module 'mongoose' {
215215
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
216216
post<T extends Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, fn: PostMiddlewareFunction<T, T>): this;
217217
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<T>>): this;
219-
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<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;
220220
post<T = M>(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<T, T>): this;
221221
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
222222

0 commit comments

Comments
 (0)