-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Closed
Copy link
Labels
Milestone
Description
Bug Report
🔎 Search Terms
predicate, discrimination
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about 4.5.2. Test in 4.0 and nightly as well.
⏯ Playground Link
💻 Code
function test(x: unknown) {
if (typeof x === "string" || x === 0) {
x
if (x === "hello" || x === 0) {
x
}
}
if (check1(x)) {
x
if (check2(x)) {
x
}
}
}
function check1(x: unknown): x is (string | 0) {
return typeof x === "string" || x === 0;
}
function check2(x: unknown): x is ("hello" | 0) {
return x === "hello" || x === 0;
}🙁 Actual behavior
In the first set of nested scopes, typeof x is narrowed to string | 0 by the first if, and then "hello" | 0 by the second if. This behavior is correct.
In the second set of nested scopes, typeof x is narrowed to string | 0 by the first if, but is narrowed to 0 by the second if.
🙂 Expected behavior
"hello" is a valid part of the subset of string, and should be included in the union type. The types derived from these two methods should be identical.
RyanCavanaugh