猫鼬';s loadClass()与TypeScript
Mongoose接受a作为模式的基础 该链接中的示例如下:猫鼬';s loadClass()与TypeScript,typescript,mongoose,mongoose-schema,Typescript,Mongoose,Mongoose Schema,Mongoose接受a作为模式的基础 该链接中的示例如下: class PersonClass { get fullName() { return `${this.firstName} ${this.lastName}`; // compiler error } } PersonSchema.loadClass(PersonClass); 该模式的属性未在类中定义,因此TypeScript编译器说: PersonClass类型上不存在属性firstName 黑客是使
class PersonClass {
get fullName() {
return `${this.firstName} ${this.lastName}`; // compiler error
}
}
PersonSchema.loadClass(PersonClass);
该模式的属性未在类中定义,因此TypeScript编译器说:
PersonClass类型上不存在属性firstName
黑客是使用虚拟构造函数:
constructor(readonly firstName: string, readonly lastName: string) { }
然而,这是黑客,更难维护
没有黑客,还有其他方法吗?诀窍是使用
这个IPerson
注释:
get fullName(this IPerson) {
return `${this.firstName} ${this.lastName}`;
}
其中,
IPerson
是该模式的对应接口。对于我的Typescript版本3.4.5,我必须使用this:IPerson
而不是this IPerson
。