Cannot find namespace 'mongoose' when creating a static function in a model in Typescript

Viewed 145

Cannot find namespace 'mongoose' when creating a static function in a model in TypescriptError in finding namespace Mongoose

The code snippet shown below gives error highlighting the Model keyword

const mongoose = require('mongoose');

interface UserAttribs{
  email:String;
  password:String;
}

interface UserModel extends mongoose.Model{
  build(attribs: UserAttribs):any;
}

const userSchema = mongoose.Schema({
  email:{
    type:String,
    required: true
  },    
  password:{
    type:String,
    required: true
  }    


});
userSchema.statics.build = (attribs:UserAttribs)=>{
  return new User(attribs);
}
const User = mongoose.model('User',userSchema);

export { User };

When we hover on 'Model' the error states 'cannot find namespace mongoose' Please help

1 Answers

This works for me (and then just use '... extends Model') but I admit I don't know why your's is not working.

import mongoose, { Connection, Document, Model Schema } from 'mongoose';

Related