Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 710
albertosh Avatar asked Jun 20 '16 15:06

albertosh


People also ask

Can Mongoose be used with TypeScript?

Mongoose introduced officially supported TypeScript bindings in v5. 11.0. Mongoose's index.

What is Mongoose model ()?

A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

What is Typegoose?

Typegoose aims to solve this problem by defining only a TypeScript interface (class), which needs to be enhanced with special Typegoose decorators (like @prop ). Under the hood it uses the Reflect & reflect-metadata API to retrieve the types of the properties, so redundancy can be significantly reduced.

What does Mongoose model return?

mongoose. model() returns a Model ( It is a constructor, compiled from Schema definitions).


2 Answers

Try:

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

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

Update:

As pointed out in comments, currently it gives a incompatible override for member from "Document", so another workaround must be used. Intersection types is a solution that can be used. That said, the following can be done:

type UserDocument = IUser & Mongoose.Document;

Alternatively, if you do not want UserDocument anymore:

// Model
let Users = Mongoose.model<IUser & Mongoose.Document>('User', userSchema);

It is worth noting that there is a side effect in this solution. The conflicting properties will have the types intersected, so IUser._id (string) & Mongoose.Document._id (any) results in UserDocument._id (any), for example.

like image 62
Natan Camargos Avatar answered Oct 24 '22 05:10

Natan Camargos


I just had this exact issue, where I wanted to keep the User interface properties as separate from Mongoose as possible. I managed to solve the problem using the Omit utility type.

Here is your original code using that type:

import { Document, Model, ObjectId } from 'mongoose';

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

export interface IUserDocument extends Omit<IUser, '_id'>, Document {}
export interface IUserModel extends Model<IUserDocument> {}

like image 41
Rees Morris Avatar answered Oct 24 '22 05:10

Rees Morris