K_Adeepa
K_Adeepa

Reputation: 3

WebStorm Error: "Type Arguments Cannot Be Inferred From Usage" in Mongoose Schema (Next.js 15 + TypeScript)

I'm encountering an issue in WebStorm while working on a Mongoose schema in a Next.js 15 project with TypeScript enabled. The error message I get is:

Type arguments cannot be inferred from usage

Any guidance or solutions would be greatly appreciated!

import { Schema, model, models } from "mongoose";

const messageSchema = new Schema(
  {
    mid: { type: String, required: true },
    sender: { type: Schema.Types.ObjectId, ref: "User", required: true },
    receiver: { type: Schema.Types.ObjectId, ref: "User", required: true },
    text: { type: String },
    readStatus: { type: Boolean, default: false },
    attachmentType: {
      type: String,
      enum: ["image", "document"], 
      default: null, 
      validate: {
        validator: function (value) {
          return value === null || ["image", "document"].includes(value);
        },
        message: "Invalid attachment type. Allowed values are 'image' or 'document'.",
      },
    },
    attachmentUrl: {
      type: String,
      validate: {
        validator: function (value) {
          return this.attachmentType ? !!value : true;
        },
        message: "Attachment URL is required when attachmentType is specified.",
      },
    },
  },
  {
    timestamps: true,
  }
);

export default models.Message || model("Message", messageSchema);

Upvotes: 0

Views: 24

Answers (0)

Related Questions