TypeScript-Node-Starter icon indicating copy to clipboard operation
TypeScript-Node-Starter copied to clipboard

Use mongoose model statics object to add method

Open Meir017 opened this issue 7 years ago • 1 comments
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);

Meir017 avatar Mar 22 '18 20:03 Meir017

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);

ifeltsweet avatar Aug 29 '18 11:08 ifeltsweet