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
Just add another way (@types/mongoose must be installed with npm install --save-dev @types/mongoose)
import { IUser } from './user.ts';
import * as mongoose from 'mongoose';
interface IUserModel extends IUser, mongoose.Document {}
const User = mongoose.model('User', new mongoose.Schema({
userName: String,
password: String,
// ...
}));
And the difference between interface and type, please read this answer
This way has a advantage, you can add Mongoose static method typings:
interface IUserModel extends IUser, mongoose.Document {
generateJwt: () => string
}