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

151
Vistas
Overriding a method with different parameters in Typescript

While reading the method overriding part of the Typescript documentation, I came across something interesting. Normally the method of base class would not be referenced while overriding method in javascript. But it is different in typescript.

Example

class Base {
  greet() {
    console.log("Hello, world!");
  }
}

class Derived extends Base {

  /* 
  Property 'greet' in type 'Derived' is not assignable to the same   property in base type 'Base'.
  Type '(name: string) => void' is not assignable to type '() =>     void'.
  */
  greet(name: string) {
    console.log(`Hello, ${name.toUpperCase()}`);
  }
}

Actually, it can be done this way.

class Derived extends Base {
  greet(name?: string) {
    if (name === undefined) {
      super.greet();
    } else {
      console.log(`Hello, ${name.toUpperCase()}`);
    }
  }
}

But why? Is this a pattern? Do I have to do it this way when overriding method?

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

0

"Assignable" in typescript simply means that the parameter and return types overlap.

You have these signatures:

greet(): void;
greet(name: string): void;

Since the parameter types don't overlap, typescript warns you that the signature is not assignable to the base method.

If you change the second method to greet(name?: string): void;

then the parameter types overlap (? means a parameter is optional)) and typescript doesn't need to warn you, because you need to manually check if name is available or not.

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