Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda