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