Is there a way to mitigate the order of optional parameters in TypeScript? I currently have the class
export default class Area {
private name: string;
private description: string;
private item?: Item;
private hazard?: Hazard;
constructor(name: string, description: string, item?: Item, hazard?: Hazard) {
this.name = name;
this.description = description;
this.item = item;
this.hazard = hazard;
}
}
For this Area class, I required the name and string parameters but not the item or hazard parameters. I tried instantiating an Area object in the following ways:
let item = new Item(); // had required parameters, but not important for now
let hazard = new Hazard(); // had required parameters, but not important for now
let area = new Area("test", "test"); // works as expected
let area1 = new Area("test", "test", item); // works as expected
let area2 = new Area("test", "test", hazard); // DOES NOT WORK as expected
let area3 = new Area("test", "test", item, hazard); // works as expected
Even though hazard and item are optional, if I want to omit item, I need to pass in undefined for the third parameter. Is there anyway to mitigate or forego this behavior, where we can pass in a third argument that matches any of the optional parameters?
{ name: string, description: string, item?: Item, hazard?: Hazard }