Closed as not planned
Description
π Search Terms
"discriminated union", "discriminated union using non-literal-type"
π Version & Regression Information
β― Playground Link
π» Code
type Foo = {
a: string;
b: string;
};
type Bar = {
a: number;
b: number;
};
type Uinon = Foo | Bar;
function test({ a, b }: Uinon) {
if (typeof a === "string") {
a; // a is string
b; // b is string | number
}
}
π Actual behavior
Inside the test function, b's type does not narrow down to string when a type is string.
why discriminated union only works with literal type?
π Expected behavior
i think b's type should be string