asheinfeld asheinfeld - 5 months ago 535
Node.js Question

Mongoose + Typescript -> Exporting model interface

I want to export only my model's interfaces instead of the Document so that nobody can modify my model if it's not inside it's own class methods. I have defined the interface and the schema like this:

IUser:

interface IUser {
_id: string;
name: string;
email: string;
created_at: number;
updated_at: number;
last_login: number;
}


And the Schema:

let userSchema: Mongoose.Schema = new Mongoose.Schema({
'name': String,
'email': String,
'created_at': {'type': Date, 'default': Date.now},
'updated_at': {'type': Date, 'default': Date.now},
'last_login': {'type': Number, 'default': 0},
});

interface UserDocument extends IUser, Mongoose.Document {}


And then the model

// Model
let Users: Mongoose.Model<UserDocument> = Mongoose.model<UserDocument>('User', userSchema);


So i just export the IUser and a class User that basically has all the methods to update my model.

The problem is that typescript complains if i add the _id to my interface, but i actually need it, otherwise i will need to pass the UserDocument and that's what i didn't wanted to do. The error typescript gives me is:

error TS2320: Interface 'UserDocument' cannot simultaneously extend types 'IUser' and 'Document'.
Named property '_id' of types 'IUser' and 'Document' are not identical.


Any ideas how i can add the _id property to my interface?

Thanks!

Answer

Try:

interface UserDocument extends IUser, Mongoose.Document {
   _id: string;
}

It will resolve the conflict between IUser._id (string) vs Mongoose.Document._id (any).