Mongoose static Model definitions in Typescript

Viewed 5897

I created a Mongoose Schema and added some static methods for the Model, named Campaign.

If I console.log Campaign I can see the methods present on it. The problem is I don't know where to add those methods so that Typescript is also aware of them.

If I add them to my CampaignModelInterface, they are only available for instances of the model (or at least TS thinks they are).

campaignSchema.ts

  export interface CampaignModelInterface extends CampaignInterface, Document {
      // will only show on model instance
  }

  export const CampaignSchema = new Schema({
      title: { type: String, required: true },
      titleId: { type: String, required: true }
      ...etc
  )}

  CampaignSchema.statics.getLiveCampaigns = Promise.method(function (){
      const now: Date = new Date()
      return this.find({
           $and: [{startDate: {$lte: now} }, {endDate: {$gte: now} }]
      }).exec()
  })

  const Campaign = mongoose.model<CampaignModelInterface>('Campaign', CampaignSchema)
  export default Campaign

I also tried accessing it via Campaign.schema.statics, but without luck.

Can anyone advise how to let TS know about the methods present on the Model, not the Model instances?

1 Answers
Related