17

I 'm trying to bind my Model with a mongoose schema using Typescript. I have my IUser interface:

export interface IUser{

   _id: string;

   _email: string;
}

My User class:

export class User implements IUser{
  _id: string;
  _email: string;
}

My RepositoryBase:

export class RepositoryBase<T extends mongoose.Document> {

 private _model: mongoose.Model<mongoose.Document>;

  constructor(schemaModel: mongoose.Model<mongoose.Document>) {
     this._model = schemaModel;
  }

 create(item: T): mongoose.Promise<mongoose.model<T>> {
    return this._model.create(item);
 }
}

And finally my UserRepository which extends RepositoryBase and implements an IUserRepository (actually empty):

export class UserRepository  extends RepositoryBase<IUser> implements     IUserRepository{

  constructor(){
    super(mongoose.model<IUser>("User", 
        new mongoose.Schema({
            _id: String,
            _email: String,
        }))
    )
  }

}

Thr problem is that typescript compiler keeps saying : Type 'IUser' does not satisfy the constraint 'Document'

And if I do:

export interface IUser extends mongoose.Document

That problem is solved but the compiler says: Property 'increment' is missing in type 'User'

Really, i don't want my IUser to extend mongoose.Document, because neither IUser or User should know about how Repository work nor it's implementation.

3
  • 1
    See if this helps: github.com/Appsilon/styleguide/wiki/mongoose-typescript-models Commented Sep 12, 2016 at 17:23
  • 1
    Thanks Harry, that was very useful. But the "export mongoose.model<IUserModel>("User", userSchema)" i should do it in my User class. But then i loose all my methods, isn't it? Commented Sep 12, 2016 at 17:46
  • Stuck with the same issue. Could anyone help? Commented May 24, 2019 at 6:08

1 Answer 1

11

I solved the issue by referencing this blog post.

The trick was to extends the Document interface from mongoose like so:

import { Model, Document } from 'mongoose';

interface User {
  id: string;
  email: string;
}

interface UserModel extends User, Document {}

Model<UserModel> // doesn't throw an error anymore

Sign up to request clarification or add additional context in comments.

1 Comment

FYI: although extending the Document will make the code work properly but keep in mind that doing so will cause major performance issues, especially related to type inference, also that approach is not recommended officially Refer: mongoosejs.com/docs/6.x/docs/… . I also had used the same approach earlier (due to Zod schema validation & some custom logic) but now I have switched to automatic type inference which is officially recommended by Mongoose Refer: mongoosejs.com/docs/typescript/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.