简体   繁体   中英

Typescript error: type this is not assignable to type this

I am new to typescript and I am working on one project and I am trying to set up the project and I got an error from backend typescript I don't know how I solve it if someone knows that help me.

TS File

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;

Error

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);

I resolve it and the answer is here.

Before

const Status: Model<IStatus> = model("gen_Status", GenStatus);

After

const Status: Model<IStatus> = model<IStatus>("gen_Status", GenStatus);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM