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

126
Vistas
Typescript Function Arguement with Child/Inherited class?

I am trying to write a function that sets some properties on its arguments (not return a new object), the argument is of a class that extends from the same basecalss ie

class BaseEvent {
    string eventName!;
}

class DeleteEvent extends BaseEvent {
    string deleteReason;
}

class UpdateEvent extends BaseEvent {
    string updatedBy;
}

const modifyEvent = async ( event: BaseEvent){
switch event.eventName{
case "deleteEvent":
event.deleteReason="xyz"; //doesn't work deleteReason doesn't exist on base class
return;

case "updateEvent":
event.updatedBy="abc";
return;
}
}

and so I tried this:

switch (true){
case event instanceOf DeleteEvent:
event.deleteReason="xyz"; //doesn't work deleteReason doesn't exist on base class
return;

case event instanceOf UpdateEvent: //still doesn't work
event.updatedBy="abc";
return;
}

I've also tried typecasting

let modifiedEvent= event; //argument event
switch event.eventName{
case "updatedEvent":
modifiedEvent=<UpdatedEvent> modifiedEvent; //no luck here either
}

Now there should be a simple/clean solution for this but sadly my patience is exhausting. Any suggestion would be very helpful. Again I want to take an object of child class, see if its of childA or childB and if its childA then i update one property and if its childB then i update another property and these properties don't exist on both child types

EDIT: Also the function is async because based on what child class this is I'm doing some database read operations etc

about 4 years ago · Juan Pablo Isaza
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