I'm trying to follow the instructions given in the Mongoose documentation for their support of TypeScript: https://mongoosejs.com/docs/typescript.html.
In the docs, they provide the following example:
import { Schema, model, connect } from 'mongoose';
// 1. Create an interface representing a document in MongoDB.
interface User {
name: string;
email: string;
avatar?: string;
}
// 2. Create a Schema corresponding to the document interface.
const schema = new Schema<User>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});
// 3. Create a Model.
const UserModel = model<User>('User', schema);
They also provide an alternative example where the interface is extending Document:
interface User extends Document {
name: string;
email: string;
avatar?: string;
}
They recommend not to use the approach of extending the Document. However, when I try their exact code (without extends) I get the following errors:
Type 'User' does not satisfy the constraint 'Document<any, {}>'.
Type 'User' is missing the following properties from type 'Document<any, {}>': $getAllSubdocs, $ignore, $isDefault, $isDeleted, and 47 more.
I'm using Mongoose v 5.12.7. Is there something I don't understand about this? How can I create the schema without extending the document? I want to test it later on, and I don't want to have to mock 47 or more properties...