Mongoose the Typescript way…?

后端 未结 14 583
遥遥无期
遥遥无期 2020-11-28 19:27

Trying to implement a Mongoose model in Typescript. Scouring the Google has revealed only a hybrid approach (combining JS and TS). How would one go about implementing the

14条回答
  •  忘掉有多难
    2020-11-28 20:15

    Here's how I do it:

    export interface IUser extends mongoose.Document {
      name: string; 
      somethingElse?: number; 
    };
    
    export const UserSchema = new mongoose.Schema({
      name: {type:String, required: true},
      somethingElse: Number,
    });
    
    const User = mongoose.model('User', UserSchema);
    export default User;
    

提交回复
热议问题