-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: "this" of Schema's instance methods #14028
fix: "this" of Schema's instance methods #14028
Conversation
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.
@ruxxzebre Looks nice! Could you implement some tests that check the type of this
in methods, both when you are not passing InstanceMethods
and when you are passing your desired instance?
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.
@ruxxzebre Also please look at this line:
https://github.com/Automattic/mongoose/blob/master/types/index.d.ts#L300
I guess we do not have typing in this situation because of this line:
schema.methods.fullName = function fullName() {
expectType<string>(this.firstName);
return ''
}
And maybe re-use your implementation so the type of this
methods stays consistent.
I seem to have found similar behaviour with statics, as well as query helpers. ProjectSchema.query.byName = function byName(
this: QueryWithHelpers<any, HydratedDocument<Project>, ProjectQueryHelpers>,
name: string
) {
return this.find({ name: name });
}; With AddThisParameter we can help with typings of those also. What you think? |
@ruxxzebre PR Looks nice. Yes, if you can fix those two, why not trying it! 😊 |
This is excellent, thanks for this PR. We've been struggling to figure out how to implement something like the |
Feel free to put in a separate PR for the query helper changes you mentioned in #14028 (comment) |
Summary
Fixes bug described in this issue.
Examples
Before
After