Confusion over the logic of "This condition will always return 'false' since the types '"a"' and '"b"' have no overlap."

Viewed 2853

I just started learning TypeScript, and I'm currently reading the handbook. One of the points it makes it that TypeScript catches logic errors, and it gives this example:

const value = Math.random() < 0.5 ? "a" : "b";
if (value !== "a") {
  // ...
} else if (value === "b") {
  // This condition will always return 'false' since 
  // the types '"a"' and '"b"' have no overlap.
  // Oops, unreachable
}

Probably there's just something I'm not seeing straight, but I can't get why this should throw an error. Here's how I see it:

  • value can be either a or b
  • The first if statement checks to see if value isn't a
  • The else-if statement checks if the value is b, which it would always be if it wasn't a (if value is not a; it's b)

So why does it say value === "b" will always return false? Obviously this doesn't work the way I'm looking at it; it seems like there's something I'm missing. Also I'm not totally sure what overlap in '"a"' and '"b"' have no overlap means, so if someone could help me understand these, it'd be greatly appreciated.

0 Answers
Related