Mongoose typing issue with typescript
For TypeScript@2.0I think you may use
npm install @types/mongoose --save
instead of:
npm install @typings/mongoose --save
This is full example:
Database.ts
import mongoose = require('mongoose');mongoose.Promise = global.Promise;mongoose.connect('mongodb://admin:123456@ds149437.mlab.com:49437/samples');export { mongoose };
UserData.ts
import { mongoose } from './../../Services/Database';export interface UserData { is_temporary: boolean; is_verified: boolean; status: boolean; username: string;}export interface IUserData extends UserData, mongoose.Document, mongoose.PassportLocalDocument { };
UserModel.ts
import { IUserData } from './UserData';import { mongoose } from './../../Services/Database';import * as passportLocalMongoose from 'passport-local-mongoose';import Schema = mongoose.Schema;const UserSchema = new Schema({ username: { type: String, required: true }, password: String, status: { type: Boolean, required: true }, is_verified: { type: Boolean, required: true }, is_temporary: { type: Boolean, required: true }});UserSchema.plugin(passportLocalMongoose);var UserModel;try { // Throws an error if 'Name' hasn't been registered UserModel = mongoose.model('User')} catch (e) { UserModel = mongoose.model<IUserData>('User', UserSchema);}export = UserModel;
I also full project example using typescript, node.js, mongoose & passport.js right here: https://github.com/thanhtruong0315/typescript-express-passportjs
Good luck.