Within my interface, I have a property that can be optional. In the constructor, I set default values for this property, which are then overridden by values passed in as the first parameter. If no properties are set, the defaults are used.
I am looking for a way to keep the parameter optional when passed into the constructor but ensure that it is considered as set within the class. I want to avoid receiving the error:
Object is possibly 'undefined'.
export interface IMain {
req: string;
prop?: ISecondary;
}
export interface ISecondary {
a?: string;
b?: string;
}
export class ABC {
public main: Main;
public constructor(main: Main) {
this.main.prop = {
a: "A",
b: "B",
...main.prop
};
}
public doSomething() {
this.main.prop.a = "Z";
}
}
new ABC({
req: 'cat'
});