I'm facing an issue with a typescript class that has an interface implemented in the constructor parameter:
interface responseObject {
a: string;
b: boolean;
c?: boolean;
}
class x
{
a: string;
b: boolean;
c: boolean;
constructor(args: responseObject) {
//checking if non-optional parameters are not supplied
if(!args.a || !args.b) throw new Error("Error");
//Attempting to initialize class data member -> but it's not working
this.a = args.a;
this.b = args.b;
}
}
Now, I have two questions. My first question is how can I access the different parameters supplied inside args. For example, how could I check if they are both supplied (similar to what I tried with !args.a
or !args.b
). And my second question is how can I provide a default value for c
if it is not supplied?
Thank you in advance.