【发布时间】:2018-10-09 19:30:37
【问题描述】:
我正在学习 TypeScript 并与猫鼬玩耍。我有以下 Schema 定义(简化形式):
interface IContact extends Document {
type: string;
firstName?: string;
}
const contactSchema = new mongoose.Schema({
contactType: {
type: String,
required: true,
trim: true,
enum: ['person', 'general']
},
firstName: {
type: String,
required: function() {
return this.contactType === 'person';
},
minlength: 1,
trim: true
}})
const Contact = mongoose.model<IContact>('Contact', contactSchema);
现在,TypeScript 编译器给了我以下错误:
“Schema |”类型上不存在属性“contactType”模式类型 | SchemaTypeOpts'。 类型“Schema”上不存在属性“contactType”。
是否以这种方式验证必填字段,取自猫鼬文档,正确(使用 this 关键字),还是有其他方法?
或者,我应该以某种方式注释我的 Schema,TypeScript 会知道我的 Schema 中存在 contactType 属性吗?
【问题讨论】:
标签: node.js mongodb typescript mongoose