Typescript中的Mongoose静态模型定义

Typescript中的Mongoose静态模型定义,typescript,mongoose-schema,mongoose-models,Typescript,Mongoose Schema,Mongoose Models,我创建了一个Mongoose模式,并为模型添加了一些静态方法,名为Campaign 如果我使用console.log活动,我可以看到它上存在的方法。问题是我不知道在哪里添加这些方法,以便Typescript也知道它们 如果我将它们添加到我的CampaignModelInterface中,它们仅可用于模型的实例(或者至少TS认为它们是可用的) 活动模式.ts export interface CampaignModelInterface extends CampaignInterface, D

我创建了一个Mongoose模式,并为模型添加了一些静态方法,名为Campaign

如果我使用console.log活动,我可以看到它上存在的方法。问题是我不知道在哪里添加这些方法,以便Typescript也知道它们

如果我将它们添加到我的CampaignModelInterface中,它们仅可用于模型的实例(或者至少TS认为它们是可用的)

活动模式.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
导出接口活动模型接口扩展活动接口,文档{
//将仅在模型实例上显示
}
export const activeschema=新架构({
标题:{type:String,必需:true},
titleId:{type:String,必需:true}
等
)}
CampaignSchema.statics.getLiveCampaigns=Promise.method(函数(){
const now:Date=新日期()
把这个还给我({
$and:[{startDate:{$lte:now},{endDate:{$gte:now}]
}).exec()
})
const-Campaign=mongoose.model('Campaign',CampaignSchema)
导出默认活动
我还尝试通过Campaign.schema.statics访问它,但运气不好


有人能建议如何让TS知道模型上的方法,而不是模型实例吗?

我回答了一个非常类似的问题,尽管我将回答您的问题(主要是我其他答案的第三部分),因为您提供了不同的模式。有一本关于猫鼬打字的自述书很有帮助,但其中有一节是关于猫鼬打字的


您描述的行为完全正常-Typescript被告知模式(描述单个文档的对象)具有名为
getLiveCampaigns
的方法

相反,您需要告诉Typescript方法在模型上,而不是模式上。完成后,您可以按照普通Mongoose方法访问静态方法。您可以通过以下方式完成此操作:

// CampaignDocumentInterface should contain your schema interface,
// and should extend Document from mongoose.
export interface CampaignInterface extends CampaignDocumentInterface {
    // declare any instance methods here
}

// Model is from mongoose.Model
interface CampaignModelInterface extends Model<CampaignInterface> {
    // declare any static methods here
    getLiveCampaigns(): any; // this should be changed to the correct return type if possible.
}

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()
})

// Note the type on the variable, and the two type arguments (instead of one).
const Campaign: CampaignModelInterface = mongoose.model<CampaignInterface, CampaignModelInterface>('Campaign', CampaignSchema)
export default Campaign
//活动文档接口应包含您的架构接口,
//并且应该从mongoose扩展文档。
导出接口活动接口扩展活动文档接口{
//在此声明任何实例方法
}
//模型来自猫鼬
接口活动模型接口扩展模型{
//在此声明任何静态方法
getLiveCampaigns():any;//如果可能,应将其更改为正确的返回类型。
}
export const activeschema=新架构({
标题:{type:String,必需:true},
titleId:{type:String,必需:true}
//…等等
)}
CampaignSchema.statics.getLiveCampaigns=Promise.method(函数(){
const now:Date=新日期()
把这个还给我({
$and:[{startDate:{$lte:now},{endDate:{$gte:now}]
}).exec()
})
//请注意变量的类型和两个类型参数(而不是一个)。
const Campaign:CampaignModelInterface=mongoose.model('Campaign',CampaignSchema)
导出默认活动

谢谢!奇怪的是,我以前没有想到你原来的答案:)能够按照你的建议让它工作。如果我没有弄错的话,我现在将把所有Schema.static方法放在活动模型接口上,把所有Schema.method方法放在活动文档接口上?我个人已经设置好了它,以便
活动文档接口
只包含模式(定义见
活动模式
)<代码>活动接口包含所有的
架构。方法
方法,而
活动模型接口
包含所有的
架构。静态
方法。您也可以在
活动文档接口
中声明您的
架构。方法
方法,我个人更喜欢分开。确保你放弃订单:
model
。这将不起作用:
model
@Mykola键入文件中的
model
的函数模型是
函数模型。因此,订单为
型号(…)