Hey guys I was wondering if there was a correct way to do this. I am currently trying to define a type of variable based on conditional logic but am currently running into issues. Both Interface1 and Interface2 are defined as interfaces in Typescript. With the first console.log of the type of the eventProps variable I get an undefined which I thought I would originally log out the type of: 'Interface1 | Interface2'. And with the second console.log I log out is of type object which I thought would log out either 'Interface1' or 'Interface2'. Am I doing something wrong/is there a better way to do what I am trying to accomplish?
let eventProps: Interface1 | Interface2;
console.log(typeof eventProps);
switch (event) {
case 'event1':
eventProps = { isCorrect: true, name: 'Mike' } as Interface1;
default:
eventProps = { isCorrect: true } as Interface2;
}
console.log(typeof eventProps);