Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
Prevent TypeScript public function calling a private function

I have this class:

export class ResourceFactory {
    AlgoliaAppId = "AlgoliaAppId";
    // ...

    private resetParams() {
        this.AlgoliaAppId = "AlgoliaAppId";
        // ...
    }

    public initTemplate(objectName, directiveArgs): Template {
        this.resetParams();  // <-- Can I, in any possible way, prevent this line from running?

        this.AlgoliaAppId = `${this.AlgoliaAppId}${objectName}`;
        // ... [long function content which I don't want to duplicate]
    }
}

I'm trying to extend the ResourceFactory class: I want to change the AlgoliaAppId name and prevent resetParams from running. (I cannot edit the original class).

Is there any way to override resetParams even though it's private, or at least somehow monkey-patch the initTemplate method so it won't run the line this.resetParams?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There's no (clean) way to override a private method from a base class you don't have control over. I tried some module augmentation to see if I could change the modifier to protected but didn't have much luck; TypeScript seems to want all overloads to have the same modifier.

However, I was able to patch the prototype after the class declaration to hack in an overload, at the cost of including a @ts-expect-error comment. Seems to be the path of least resistance for fighting with the compiler. Here's what an example subclass would look like:

class Example extends ResourceFactory {
    AlgoliaAppId = "CustomName";
}

// @ts-expect-error
Example.prototype.resetParams = function resetParams() { 
    // your implementation
}

Here's a link to the playground as well.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!