没有重载与打字稿、nodejs 和猫鼬

时间:2021-02-11 04:34:55

标签: node.js mongodb typescript mongoose

我在以前运行的应用程序中有一项服务,但突然间我收到此打字稿错误,我不确定如何解决它。请查看我的模型文件:

import mongoose from "mongoose";
import { AccessType, TicketType } from "./types";

interface TicketAttrs {
  name: string;
  type: TicketType;
  access: AccessType;
  uniqueId: string;
  price: number;
  quantity: number;
  creator: string;
}

interface TicketDoc extends mongoose.Document {
name: string;
type: TicketType;
access: AccessType;
uniqueId: string;
price: number;
quantity: number;
creator: string;
}

interface TicketModel extends mongoose.Model<TicketDoc> {
build(attrs: TicketAttrs): TicketDoc;
}

const ticketSchema = new mongoose.Schema(
 {
  name: {
  type: String,
},
type: {
  type: String,
  enum: Object.values(TicketType),
},
access: {
  type: String,
  enum: Object.values(AccessType),
},
price: {
  type: Number,
},
uniqueId: {
  type: String,
},
quantity: {
  type: Number,
},
creator: {
  type: String,
 },
},
{
timestamps: true,
toJSON: {
  transform(doc, ret) {
    ret.id = ret._id;
    delete ret._id;
  },
},
 }
);

ticketSchema.statics.build = (attrs: TicketAttrs) => {
 return new Ticket(attrs);
};

const Ticket = mongoose.model<TicketDoc, TicketModel>("Ticket", ticketSchema);

export { Ticket };

我在此行的 ticketSchema 上遇到错误 const Ticket = mongoose.model<TicketDoc, TicketModel>("Ticket", ticketSchema);

错误信息是:-

const ticketSchema: mongoose.Schema<mongoose.Document<any>, mongoose.Model<mongoose.Document<any>>>
No overload matches this call.
  Overload 1 of 6, '(name: string, schema?: Schema<TicketDoc, TicketModel> | undefined, collection?: string | undefined, skipInit?: boolean | undefined): TicketModel', gave the following error.
    Argument of type 'Schema<Document<any>, Model<Document<any>>>' is not assignable to parameter of type 'Schema<TicketDoc, TicketModel>'.
      Types of property 'methods' are incompatible.
        Type '{ _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; $isEmpty: (path: string) => boolean; ... 47 more ...; validateSync: (pathsToValidate?: string[] | undefined, options?: any) => NativeError | null; } & { ....' is not assignable to type '{ name: string; type: TicketType; access: AccessType; uniqueId: string; price: number; quantity: number; creator: string; _id?: any; __v?: number | undefined; $ignore: (path: string) => void; ... 50 more ...; validateSync: (pathsToValidate?: string[] | undefined, options?: any) => NativeError | null; } & { ...; }'.
          Type '{ _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; $isEmpty: (path: string) => boolean; ... 47 more ...; validateSync: (pathsToValidate?: string[] | 

我也收到一条错误消息,说没有构建函数,我不明白是什么破坏了代码,它在没有任何更改的情况下运行。

0 个答案:

没有答案