[英]Mongoose Schema with Typescript - Design errors
I have two problems defining schema using mongoose and typescript.我在使用 mongoose 和 typescript 定义模式时遇到两个问题。 Here is my code:
这是我的代码:
import { Document, Schema, Model, model} from "mongoose";
export interface IApplication {
id: number;
name: string;
virtualProperty: string;
}
interface IApplicationModel extends Document, IApplication {} //Problem 1
let ApplicationSchema: Schema = new Schema({
id: { type: Number, required: true, index: true, unique: true},
name: { type: String, required: true, trim: true },
});
ApplicationSchema.virtual('virtualProperty').get(function () {
return `${this.id}-${this.name}/`; // Problem 2
});
export const IApplication: Model<IApplicationModel> = model<IApplicationModel>("Application", ApplicationSchema);
First of all:首先:
interface IApplicationModel extends Document, IApplication {}
The Typescript is telling me that:打字稿告诉我:
error TS2320: Interface 'IApplicationModel' cannot simultaneously extend types 'Document' and 'IApplication'. Named property 'id' of types 'Document' and 'IApplication' are not identical.
So how to change definition of id
property?那么如何更改
id
属性的定义呢?
Problem 2 is in inner function (getter for virtualProperty
):问题 2 是内部函数(
virtualProperty
getter):
return `${this.id}-${this.name}/;返回`${this.id}-${this.name}/; // Problem 2
// 问题 2
The Error is:错误是:
error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
How to define type of this
?如何定义
this
类型?
Problem #1: Since IApplicationModel
extends interfaces Document
and IApplication
that declare the id
property with different types ( any
and number
respectively), TypeScript does not know whether the id
property of IApplicationModel
should be of type any
or number
. 问题1:由于
IApplicationModel
延伸接口Document
和IApplication
该声明id
与不同类型(属性any
和number
分别地),打字原稿不知道是否id
的属性IApplicationModel
应类型的any
或number
。 You can fix this by redeclaring the id
property in IApplicationModel
with the desired type. 您可以通过在
IApplicationModel
中用所需的类型重新声明id
属性来解决此问题。 (Why are you declaring a separate IApplication
interface rather than just declaring IApplicationModel
that extends Document
with all your properties?) (为什么要声明一个单独的
IApplication
接口,而不是仅仅声明使用所有属性扩展Document
IApplicationModel
?)
Problem #2: Just declare the this
special parameter to the function as shown below. 问题2:只需向函数声明
this
特殊参数,如下所示。
import { Document, Schema, Model, model} from "mongoose";
export interface IApplication {
id: number;
name: string;
virtualProperty: string;
}
interface IApplicationModel extends Document, IApplication {
id: number;
}
let ApplicationSchema: Schema = new Schema({
id: { type: Number, required: true, index: true, unique: true},
name: { type: String, required: true, trim: true },
});
ApplicationSchema.virtual('virtualProperty').get(function (this: IApplicationModel) {
return `${this.id}-${this.name}/`;
});
export const IApplication: Model<IApplicationModel> = model<IApplicationModel>("Application", ApplicationSchema);
for problem 2: just declare this
as any
: .get(function (this: any) {});
对于问题2:只需要声明
this
为any
: .get(function (this: any) {});
fix it修理它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.