-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed as not planned
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
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
Additional information about the issue
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created