question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

Error in detecting type if there is a OR logic in if

See original GitHub issue

Bug 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:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Jul 20, 2022

If we could do that we would just do the right thing instead. Detecting that condition is the problem in the first case.

1reaction
r1sicommented, Jul 20, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found