Error in detecting type if there is a OR logic in if
See original GitHub issueBug Report
๐ Search Terms
- detect type
- OR logic
- ||
๐ Version & Regression Information
This appear in all recent TS. (demo is with 4.4.4)
โฏ Playground Link
Playground link with demo of bug
๐ป Code
All code with comment is already in playground link watch error on line 19 of index.ts But here:
import { TypeOfMain, getMainObj } from "./external";
let mainObject: false | TypeOfMain = false;
mainObject = getMainObj();
const randomAlternative = Math.random();
// with OR in IF error on line 20
if (
randomAlternative > 0.5 ||
(mainObject && Number(mainObject.value) <= 250)
) {
if (randomAlternative > 0.5) {
console.log(`Error in random ${randomAlternative}`);
} else {
// error mainObject must be TypeOfMain not false
console.log(`Error in main ${mainObject.value}`); // <--- Error
}
}
// with double IF no error on line 31
if (randomAlternative > 0.5) {
if (mainObject && Number(mainObject.value) <= 250) {
if (randomAlternative > 0.5) {
console.log(`Error in random ${randomAlternative}`);
} else {
// no error
console.log(`Error in main ${mainObject.value}`);
}
}
}
๐ Actual behavior
Error on line 19 mainObject must be TypeOfMain not false
๐ Expected behavior
Like line 31
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
if / else errors - learn how to fix these - Codecademy
Hi nicoledille, when the JavaScript interpreter is telling you that if found an unexpected else, that is likely because you have ended your...
Read more >How to correct a #VALUE! error in the IF function
When there is a cell reference to an error value, IF displays the #VALUE! error. Solution: You can use any of the error-handling...
Read more >Syntax Error - an overview | ScienceDirect Topics
Error: Character vector is not terminated properly. If this type of error is typed in a script or function using the Editor, the...
Read more >What are Error-Detecting Codes? - Tutorialspoint
Error detection involves checking whether any error has occurred or not. The number of error bits and the type of error does not...
Read more >Conditionals and logic | Think Java | Trinket
The condition in parentheses can be any boolean expression. A second form of conditional statement has two possibilities, indicated by if and else...
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
If we could do that we would just do the right thing instead. Detecting that condition is the problem in the first case.
@RyanCavanaugh Could you consider including a specific warning in all errors related to the data type within an if (scope) where its condition is written with an OR (and thus not determinable by design pattern)? Reasoning about this, I understand the limitation or at least the difficulty on the implementation of the check; but if not written it could mislead the user, who would expect (as is the case of other situations) a precise warning, especially because a TS error of this type compromises the compilation of the code.