Control Flow Analysis of Aliased Conditions and Discriminants breaks on else statement if condition not strictly boolean
See original GitHub issueBug Report
🔎 Search Terms
Control Flow Analysis of Aliased Conditions and Discriminants
🕗 Version & Regression Information
4.4+
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about control flow analysis
⏯ Playground Link
Playground link with relevant code
💻 Code
class A {}
class B {}
class C {}
declare const test: A | B | C;
const isAOrB = (test instanceof A || test instanceof B);
declare function getSomeData(input: A | B): {}
const getData = (test instanceof A || test instanceof B) && getSomeData(test);
if (isAOrB) {
test; //expect A | B - passes
} else {
test; //expect C - passes
}
if (getData) {
test; //expect A | B - passes
} else {
test; //expect C - fails
}
🙁 Actual behavior
the else statement loses it’s narrowing because getData is not strictly a boolean it seems.
🙂 Expected behavior
the if/else of getData should behave the same as the if/else of isAOrB
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Control flow analysis of aliased conditional expressions in ...
Control flow analysis of aliased conditional expressions in TypeScript ... foo is a boolean); a discriminant property of bar (i.e. bar is a ......
Read more >Announcing TypeScript 4.4 RC - Microsoft Developer Blogs
Control Flow Analysis of Aliased Conditions and Discriminants. In JavaScript, we often have to probe a variable in different ways to see if...
Read more >Get the best of TypeScript Control Flow Analysis - Retool
Control Flow Analysis is a core TypeScript feature that analyzes the code to get the best type inference depending on variable usages; ...
Read more >Typescript Type Guard wont affect the variable type if set ...
... as Control Flow Analysis of Aliased Conditions and Discriminants in the ... In the given context , if isKind1 === true then...
Read more >Control Flow in JavaScript: Conditional Statements - Medium
Logical operators are most commonly used to link multiple boolean comparison statements and return either true or false depending on the conditions. The...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Note that if
getSomeData
returns a falsy value(false
,0
,""
…), the else branch will be executed, even the input is an instance of A or B.Hm, there are two separate things happening. One is that non-literals after the
&&
eliminates the narrowing in the false branch, independent of aliasing the condition:And the second is that aliasing does behave differently when the truthy
&&
is inlined:Pretty weird.