VScode doesn't suggest TS mongoose fields types

Viewed 46

I've a simple TS IUser interface to define schema and model.

import { model, Schema } from "mongoose";


interface IUser {
  firstname: string,
  lastName: string
}


const UserSchema = new Schema({
  firstName:{type:String},
  lastName:{type:String}
})

const UserDocument = model<IUser>('User',UserSchema)

Problem VS code displays property types as any instead of string. enter image description here

I would expect it to show and allow string. Even if you add a number to firstname, it doesn't complain. I can force it by casting it to <IUser> but I haven't seen such examples. Likely, I am making a mistake.

const user = new UserDocument(<IUser>{
  firstname:'asasas',
  lastName:'asdad'
}) //shows intellisense

What's the proper way to use TS+Mongoose for static validation while typing in VSCode.

0 Answers
Related