Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda