This might be duplicated, but I cannot find an answer.
I have this type:
type A = {
prop2: string;
prop1: string;
} | {
prop3: string;
prop1: string;
}
As you can see, it has a shared prop and 2 different props. But then I can use it like this:
const a: A = {
prop1: '',
prop2: '',
prop3: '',
};
And it works!
But then I need to check that a has prop2 and prop3 to use it:
a.prop1 = '';
// @ts-expect-error
a.prop2 = ''; // ERROR
if ('prop2' in a) {
a.prop2 = ''; // OK
}
Why does typescript let me declare both props? I would expect it to be as strict as when using the variable. The original example was a React component and it let me pass all props.
BTW, I cannot add a prop4, only 1, 2 and 3.