From ed82b9ef70ab6fdf9d5d4bec1686a9662ffbc657 Mon Sep 17 00:00:00 2001 From: Karim Alibhai Date: Wed, 9 Jan 2019 00:06:49 -0500 Subject: [PATCH 1/2] Add(mongoose): generics for schema methods --- types/mongoose/index.d.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 14e965dd5f..df91675e15 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -737,7 +737,7 @@ declare module "mongoose" { * section schema.js * http://mongoosejs.com/docs/api.html#schema-js */ - class Schema extends events.EventEmitter { + class Schema extends events.EventEmitter { /** * Schema constructor. * When nesting schemas, (children in the example above), always declare @@ -789,8 +789,10 @@ declare module "mongoose" { * Adds an instance method to documents constructed from Models compiled from this schema. * If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as methods. */ - method(method: string, fn: Function): this; - method(methodObj: { [name: string]: Function }): this; + method(method: F, fn: T[F]): this; + method(methodObj: { + [F in keyof T]: T[F] + }): this; /** * Gets/sets schema paths. @@ -954,7 +956,9 @@ declare module "mongoose" { static reserved: any; /** Object of currently defined methods on this schema. */ - methods: any; + methods: { + [F in keyof T]: T[F] + }; /** Object of currently defined statics on this schema. */ statics: any; /** Object of currently defined query helpers on this schema. */ From 4f0e246373e1afbae5ddb087012893fba4dc3587 Mon Sep 17 00:00:00 2001 From: Karim Alibhai Date: Wed, 9 Jan 2019 00:35:38 -0500 Subject: [PATCH 2/2] Add: animal interface to tests --- types/mongoose/mongoose-tests.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index 7d23e6be6b..8f5914d9dd 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -522,12 +522,15 @@ mongoose.Schema.reserved.hasOwnProperty(''); /* inherited properties */ schema.addListener('e', cb); /* practical examples */ -var animalSchema = new mongoose.Schema({ +interface Animal { + findSimilarTypes(cb: any): Promise +} +var animalSchema = new mongoose.Schema({ name: String, type: String }); -animalSchema.methods.findSimilarTypes = function (cb: any) { - return this.model('Aminal').find({ type: this.type }, cb); +animalSchema.methods.findSimilarTypes = function (cb) { + return this.model('Animal').find({ type: this.type }, cb); }; var Animal: any = mongoose.model('Animal', animalSchema); var dog: any = new Animal({type: 'dog'});