Mongoose Schema is not working correctly with typescript type

Viewed 218

I am working on a project where I use Mongoose with typescript. Ive got the Problem, that Typescript is not autocompleting the required properties, when I am trying to create a new Document with Scheme.create(). I don't quite know why. It shows me a generic type on the create method as shown here:

Type

So, it just shows {} instead of my Model what I used. So lets come to the Schema and the Type i've used. I have a Type IUserModel which is in a shared space. Its shared between frontend and backend so I can't create a new Type. But thats the type:

interface IUserModel {
  password: string;
  adminLevel: Number;
  active: boolean;
  firstName: string;
  lastName: string;
  email: string;
  city?: string;
  street?: string;
  postcode?: number;
  regTimeStamp: Date;
  organizationId?: string;
  organizationAdminLevel?: number;
  balance: number;
  profilePicture?: string;
  firstLogin: boolean;
};

Now the scheme:

interface IUserModelSchema extends IUserModel, mongoose.Document{}

const UserSchema = new mongoose.Schema({
    email: { type: String, required: true, unique: true },
    firstName: { type: String, required: true },
    lastName: { type: String, required: true },
    postcode: { type: Number, required: false },
    city: { type: String, required: false },
    street: { type: String, required: false },
    password: { type: String, required: true },
    organizationId: { type: String, required: false },
    organizationAdminLevel: { type: Number, required: false },
    active: { type: Boolean, required: true },
    balance: { type: Number, required: true },
    profilePicture: { type: String, required: false },
    regTimeStamp: { type: Date, required: true },
    adminLevel: { type: Number, required: true },
});
const UserModel = mongoose.model<IUserModelSchema>("User", UserSchema);

export default UserModel;

So what I've tried:

  • I tried abandoning the shared type and creating one right above the scheme, just to test if it was something weird with the shared project threre
  • I tried using different editors
  • I tried several versions of mongoose (5.10, 5.11)

I've got another project set up with the same architecture. I am using the same Schema and there it is no problem. I get an autocompletion and the right type on the generic .create<> method there.

Edit: Okay ive found out that the last working version is 5.11.4, why is it not working anymore on the new versions?

0 Answers
Related