Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!