我是 typescript 新手,我正在开发一个项目,我正在尝试设置该项目,但我从后端 typescript 中收到一个错误,如果有人知道可以帮助我,我不知道如何解决它。
TS文件
import { Document, Model, model, Schema } from "mongoose";
import { IUser } from "./GenUser";
/**
* Interface to model GenStatus for TypeScript.
* @param cStatusCode: string
* @param cStatusName: string
* @param cStatusDesc: string
* @param iEnteredby: string
* @param tEntered: Date
* @param iUpdatedby: string
* @param tUpdated: Date;
*/
export interface IStatus extends Document {
cStatusCode: string;
cStatusName: string;
cStatusDesc: string;
iEnteredby: IUser['_id'];
tEntered: Date;
iUpdatedby: IUser['_id'];
tUpdated: Date;
}
const GenStatus: Schema = new Schema({
cStatusCode: {
type: String,
required: true,
unique: true
},
cStatusName: {
type: String,
required: true,
unique: true
},
cStatusDesc: {
type: String,
required: true
},
iEnteredby: {
type: Schema.Types.ObjectId,
ref: "User"
},
tEntered: {
type: Date
},
iUpdatedby: {
type: Schema.Types.ObjectId,
ref: "User"
},
tUpdated: {
type: Date
}
});
const Status: Model<IStatus> = model("gen_Status", GenStatus);
export default Status;
错误
src/models/GenStatus.ts:55:7 - error TS2322: Type 'Model<Document<any, any, any>, any, any>' is not assignable to type 'Model<IStatus, {}, {}>'. The types returned by 'create(...)' are incompatible between these types. Type 'Promise<Document<any, any, any>[]>' is not assignable to type 'Promise<IStatus[]>'. Type 'Document<any, any, any>[]' is not assignable to type 'IStatus[]'. Type 'Document<any, any, any>' is not assignable to type 'IStatus'. 55 const Status: Model = model("gen_Status", GenStatus);
最佳答案
我解决了,答案就在这里。
之前
const Status: Model<IStatus> = model("gen_Status", GenStatus);
之后
const Status: Model<IStatus> = model<IStatus>("gen_Status", GenStatus);
关于javascript - typescript 错误: type this is not assignable to type this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70030693/