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: "this" of Schema's instance methods #14028

Merged
merged 6 commits into from
Nov 14, 2023

Conversation

ruxxzebre
Copy link
Contributor

Summary

Fixes bug described in this issue.

Examples
Before

Screenshot 2023-11-01 at 22 57 52

After

Screenshot 2023-11-01 at 22 57 09

@ruxxzebre ruxxzebre changed the title Fix "this" of Schema's instance methods fix: "this" of Schema's instance methods Nov 1, 2023
Copy link
Contributor

@pshaddel pshaddel left a 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?

Copy link
Contributor

@pshaddel pshaddel left a 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.

@ruxxzebre
Copy link
Contributor Author

ruxxzebre commented Nov 2, 2023

I seem to have found similar behaviour with statics, as well as query helpers.
For query helpers there's example in docs on how it can be mitigated.

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?

@pshaddel
Copy link
Contributor

pshaddel commented Nov 2, 2023

@ruxxzebre PR Looks nice. Yes, if you can fix those two, why not trying it! 😊
BTW I am not a maintainer so for the merge and final review you should wait for a maintainer to look at this. But good job fixing the issue!

@vkarpov15 vkarpov15 added this to the 8.0.1 milestone Nov 14, 2023
@vkarpov15
Copy link
Collaborator

This is excellent, thanks for this PR. We've been struggling to figure out how to implement something like the AddThisParameter<> helper 👍

@vkarpov15
Copy link
Collaborator

Feel free to put in a separate PR for the query helper changes you mentioned in #14028 (comment)

@vkarpov15 vkarpov15 merged commit abb823d into Automattic:master Nov 14, 2023
3 checks passed
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

3 participants