我创建了一个Mongoose Schema并为Model添加了一些名为Campaign的静态方法.
如果我是console.log Campaign我可以看到它上面的方法.问题是我不知道在哪里添加这些方法,以便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
Run Code Online (Sandbox Code Playgroud)
我也试过通过Campaign.schema.statics访问它,但没有运气.
任何人都可以建议如何让TS了解模型中存在的方法,而不是模型实例?