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(types): better typescript support for schema plugins #12139
Conversation
02b7856
to
cb93470
Compare
types/index.d.ts
Outdated
@@ -291,15 +296,15 @@ declare module 'mongoose' { | |||
set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions[K], _tags?: any): this; | |||
|
|||
/** Adds static "class" methods to Models compiled from this schema. */ | |||
static(name: string, fn: (this: M, ...args: any[]) => any): this; | |||
static(obj: { [name: string]: (this: M, ...args: any[]) => any }): this; | |||
static(name: keyof TStaticMethods, fn: (this: M, ...args: any[]) => 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.
This change is a bit risky, because it will break compile for anyone that uses static()
without setting TStaticMethods
. Can we add a method overload that still allows calling static()
with an arbitrary string?
types/index.d.ts
Outdated
|
||
/** Creates a virtual type with the given name. */ | ||
virtual<T = HydratedDocument<DocType, TInstanceMethods>>( | ||
name: string, | ||
name: keyof TVirtuals, |
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.
Same with above. This change is a bit risky.
cb93470
to
d89a7b6
Compare
Thanks for the feedback @vkarpov15. I've updated the types and added test for this as well. |
d89a7b6
to
ca3444d
Compare
Looks like there were some merge conflict issues, because tests are failing now. Can you please patch that up @emiljanitzek ? I took a look and it looks like this change doesn't cause any significant performance degradation. |
7e5ac5d
to
d000b26
Compare
d000b26
to
faa6422
Compare
@vkarpov15 rebased and fixed the conflicts. |
Summary
Found more problems when working with typescript and plugins. Since the Schema type sets default values (empty object) for
TInstanceMethods
,TQueryHelpers
andTStaticMethods
we need to be able to pass along these types when trying to create query helpers/virtuals/methods/statics.This PR also contains the fix for Schema and model mismatch. Will rebase this if the other PR gets accepted.
Examples
Added
plugin.tests.ts
for examples