Skip to content
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

Merged
merged 1 commit into from Aug 9, 2022

Conversation

emiljanitzek
Copy link
Contributor

Summary

Found more problems when working with typescript and plugins. Since the Schema type sets default values (empty object) for TInstanceMethods, TQueryHelpers and TStaticMethods 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

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;
Copy link
Collaborator

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,
Copy link
Collaborator

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.

@emiljanitzek
Copy link
Contributor Author

Thanks for the feedback @vkarpov15. I've updated the types and added test for this as well.

@vkarpov15
Copy link
Collaborator

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.

@emiljanitzek emiljanitzek force-pushed the feature/plugin-types branch 3 times, most recently from 7e5ac5d to d000b26 Compare August 8, 2022 09:23
@emiljanitzek
Copy link
Contributor Author

@vkarpov15 rebased and fixed the conflicts.

@vkarpov15 vkarpov15 merged commit 0831ccb into Automattic:master Aug 9, 2022
@vkarpov15 vkarpov15 added this to the 6.5.2 milestone Aug 9, 2022
vkarpov15 added a commit that referenced this pull request Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants