Skip to content

Commit c289cb4

Browse files
committed
Merge branch 'master' into vkarpov15/gh-15410
2 parents b107906 + 9702ac2 commit c289cb4

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

test/types/schema.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
Query,
2323
model,
2424
ValidateOpts,
25-
BufferToBinary
25+
BufferToBinary,
26+
CallbackWithoutResultAndOptionalError
2627
} from 'mongoose';
2728
import { Binary } from 'mongodb';
2829
import { IsPathRequired } from '../../types/inferschematype';
@@ -1778,6 +1779,22 @@ function gh15301() {
17781779
});
17791780
}
17801781

1782+
function gh15412() {
1783+
const ScheduleEntrySchema = new Schema({
1784+
startDate: { type: Date, required: true },
1785+
endDate: { type: Date, required: false }
1786+
});
1787+
const ScheduleEntry = model('ScheduleEntry', ScheduleEntrySchema);
1788+
1789+
type ScheduleEntryDoc = ReturnType<typeof ScheduleEntry['hydrate']>
1790+
1791+
ScheduleEntrySchema.post('init', function(this: ScheduleEntryDoc, _res: any, next: CallbackWithoutResultAndOptionalError) {
1792+
expectType<Date>(this.startDate);
1793+
expectType<Date | null | undefined>(this.endDate);
1794+
next();
1795+
});
1796+
}
1797+
17811798
function defaultReturnsUndefined() {
17821799
const schema = new Schema<{ arr: number[] }>({
17831800
arr: {

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ declare module 'mongoose' {
387387
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PostMiddlewareFunction<T, T>): this;
388388
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
389389
post<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: PostMiddlewareFunction<T, T>): this;
390+
post<T = THydratedDocumentType>(method: 'init', fn: PostMiddlewareFunction<T, T>): this;
391+
390392
// this = Query
391393
post<T = Query<any, any>>(method: MongooseRawResultQueryMiddleware|MongooseRawResultQueryMiddleware[], fn: PostMiddlewareFunction<T, null | QueryResultType<T> | ModifyResult<QueryResultType<T>>>): this;
392394
post<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;

types/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ declare module 'mongoose' {
7070
create(obj: any): THydratedDocumentType;
7171

7272
/** Searches array items for the first document with a matching _id. */
73-
id(id: any): THydratedDocumentType | null;
73+
id(id: ObjectId | string | number | Buffer): THydratedDocumentType | null;
7474

7575
push(...args: (AnyKeys<T> & AnyObject)[]): number;
7676

0 commit comments

Comments
 (0)