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

Add guard to stop loadClass copying Document if Document is used as base of loaded class (same hack as implemented for Model already). #12820

Merged

Conversation

sgpinkus
Copy link
Contributor

Summary
I want to set Document as the base class of a loadClass class so I can inherit its typings on this conveniently. There is a guard condition hack in the code to allow this for Model already. This PR just extends to guard condition to account for possibility Document is the base class. If that didn't already exist I wouldn't PR this, but since it exists may as well account for Document too, in case people do that, and for consistency.

Examples
Something like this:

export class MyResourceMethods extends Document
{
  delete() {
    return this.remove();
  }
  async ping() {
    await this.updateOne({ $inc: { pingCount: 1 } });
    return { modified: true };
  }
 ...
}

If you want Typescript to know types for Document methods (updateOne, remove in example) you can either type this in every method or do the above. The above is more convenient.

Refs #12813.

…ase of loaded class (same hack as implemented for Model already).
@sgpinkus sgpinkus force-pushed the feat/allow-doc-as-base-for-loadclass branch from 66e466f to c63cffb Compare January 6, 2023 05:55
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15 vkarpov15 merged commit 34bda70 into Automattic:master Jan 10, 2023
@vkarpov15 vkarpov15 added this to the 6.8.4 milestone Jan 10, 2023
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