-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Add generic to plugin schema definition #9968
fix: Add generic to plugin schema definition #9968
Conversation
index.d.ts
Outdated
@@ -1123,7 +1123,7 @@ declare module 'mongoose' { | |||
pathType(path: string): string; | |||
|
|||
/** Registers a plugin for this schema. */ | |||
plugin(fn: (schema: Schema, opts?: any) => void, opts?: any): this; | |||
plugin<T extends Document = DocType, L extends Model<T> = M>(fn: (schema: Schema<T, L, SchemaDefinitionType>, opts?: any) => void, opts?: any): this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are the generics necessary? Adding schema: Schema<DocType, Model<DocType>, SchemaDefinitionType>
should be sufficient for this case, shouldn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct, it would be sufficient for the case above. I was following the same pattern as found with post/pre methods just below. I can update according to your suggestion if you would prefer that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please, I'd rather avoid adding unnecessary generic params if possible.
c6727a5
to
8788047
Compare
8788047
to
b24b917
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
Summary
Update the typescript definition for
plugin()
on Schema to add generic support. Currently, the plugin() complains when trying to add a plugin with custom Document/Model definitions.Examples