I am currently following the guidelines outlined in the Mongoose documentation to incorporate TypeScript support into my project: https://mongoosejs.com/docs/typescript.html.
Within the documentation, there is an example provided as follows:
import { Schema, model, connect } from 'mongoose';
// 1. Define a MongoDB document interface.
interface User {
name: string;
email: string;
avatar?: string;
}
// 2. Create a Schema based on the document interface.
const schema = new Schema<User>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});
// 3. Generate a Model.
const UserModel = model<User>('User', schema);
An alternative example is also presented where the interface extends Document
:
interface User extends Document {
name: string;
email: string;
avatar?: string;
}
The recommended approach advises against using the extension of Document
. However, when attempting their suggested code (without extends
), I encounter the following errors:
Type 'User' does not fulfill the constraint 'Document<any, {}>'.
Type 'User' lacks the following properties present in 'Document<any, {}>': $getAllSubdocs, $ignore, $isDefault, $isDeleted, along with 47 more.
I am utilizing version 5.12.7 of Mongoose. Is there a concept that I may be overlooking? How can I establish the schema without extending the document? My intention is to conduct tests later on, and I wish to avoid having to mock 47 or more properties...