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

296
Vistas
Typescript: How to reassign static function in child class?

I have a parent class like so:

class SomeClass {

  public someProperty: number | undefined;

  constructor(value: number) {
    this.someProperty = value;
  }

  public on(eventType: string, fn: Function) {

  }
}

class Parent {

  protected static getTransform(value: number) {
    return value + 180;
  }

  public transform: SomeClass;

  constructor(value: number) {
    this.transform = this.createTransform(value);
  }

  protected createTransform(value: number) {
    const transform = new SomeClass(value);
    transform.on('rotate', this.rotate);
    return transform;
  }

  protected rotate(event: any) {
    this.transform.someProperty = Parent.getTransform(event.transform);
  }
}

And I need to implement child class that has different logic of transform property calculation. I need something, like so:

class Child extends Parent {

  protected static getTransform(value: number) {
    return value + 90;
  }

  constructor(value: number) {
    super(value);
  }
}

I haven't faced any errors or something else, but my approach doesn't work, nothing changed. How to implement same? Playground

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

... protected (which allows access from the same class and its subclasses, but not objects of a different class) ... Wiki: OOP

To clear the Member Visibility concept, here is a demo of your code (Please see the comments):

class SomeClass {

  public someProperty: number | undefined;

  constructor(value: number) {
    this.someProperty = value;
  }

  public on(eventType: string, fn: Function) {

  }
}

class Parent {

  protected static getTransform(value: number) {
    return value + 180;
  }

  public transform: SomeClass;

  constructor(value: number) {
    this.transform = this.createTransform(value);
  }

  protected createTransform(value: number) {
    const transform = new SomeClass(value);
    transform.on('rotate', this.rotate);
    return transform;
  }

  protected rotate(event: any) {
    this.transform.someProperty = Parent.getTransform(event.transform);
  }
}

class Child extends Parent {

  protected static getTransform(value: number) {
    return value + 90;
  }

  constructor(value: number) {
    super(value);
  }
}

class Example extends Child {
  constructor(value: number) {
      super(value)
    console.log(`In Example:`, Child.getTransform(700)) // this line will be called when `new Example()`
  }

  public static getTransform(value: number) {
    return Child.getTransform(600); // call parent method which is protected is valid
  }

}

// Example use cases:

console.log(`Call Child:`, Child.getTransform(800)) //  this will throw error:
// Property 'getTransform' is protected and only accessible within class 'Child' and its subclasses.

new Example(700) // inheritance can call parent method which is protected

console.log(`Call Example:`, Example.getTransform(1000)) // a function call that call parent protected method is ok.

TypeScript Playground <- You can click the Run button the see the result and the Errors tab to see the error messages.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I am not sure why you want to reassign a static function.. in your case you should override the rotate() function in Child and use the Child static method instead:

  protected rotate(event: any) {
    this.transform.someProperty = Child.getTransform(event.transform);
  }
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