【发布时间】:2018-01-18 17:31:25
【问题描述】:
我创建了一个 Mongoose Schema 并为模型添加了一些静态方法,命名为 Campaign。
如果我 console.log 活动,我可以看到上面存在的方法。问题是我不知道在哪里添加这些方法,以便 Typescript 也知道它们。
如果我将它们添加到我的 CampaignModelInterface,它们仅可用于模型实例(或者至少 TS 认为它们是可用的)。
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
我也尝试通过 Campaign.schema.statics 访问它,但没有运气。
谁能建议如何让 TS 知道模型上存在的方法,而不是模型实例?
【问题讨论】:
标签: typescript mongoose-schema mongoose-models