TypeScript-Node-Starter
TypeScript-Node-Starter copied to clipboard
Use mongoose model statics object to add method
trafficstars
by adding static methods to mongoose's schema object it will show how to take advantage of the mongoose.model function even more.
using the function:
function model<T extends Document, U extends Model<T>>
this would require us to create a static method and another type
export type UserSchema = mongoose.Model<UserModel> & {
someStaticMethod: (arg1: string) => void;
}
and then at the end:
const User = mongoose.model<UserModel, UserSchema>("User", userSchema);
Is it possible to achieve the same with just JSDoc using @typedef and @type annotations? I can't seem to force vscode to understand that I want to use the overridden form of mongoose.model that has two type parameters.
/**
* @typedef {mongoose.Model<UserModel> & {someStaticMethod: (arg1: string) => void;}} UserSchema
*/
/**
* @type {UserSchema}
*/
const User = mongoose.model('User', userSchema);