why in TestErr function does the variable testvar inside the if-else block give the type string | number?
Seems it works only without default value. But the variant seems correct in both cases.
type A = {
variant?: "1" | "2";
testvar: number;
}
type B = {
variant: "99"
testvar: string;
}
type TestProps =
| A
| B
function TestErr({variant = "1", testvar}: TestProps) {
if (variant === "99") {
if (variant === "1") {} // error as expected
testvar; // testvar: string | number
} else {
if(variant === "99") {} // error as expected
testvar; // testvar: string | number
}
testvar; // testvar: string | number
}
function TestOk({variant, testvar}: TestProps) {
if (variant === "99") {
if (variant === "1") {} // error as expected
testvar; // testvar: string
} else {
if(variant === "99") {} // error as expected
testvar; // testvar: string
}
testvar; // testvar: string | number
}