Typescript says types a and b have no overlap What does it mean

0 votes
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
}

Found this code in a tutorial. I want to understand the meaning of this error: "types '"a"' and '"b"' have no overlap."

What are they talking about?

Jul 5, 2022 in TypeSript by Logan
• 2,140 points
9,331 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
Love the fact that when I finally find somebody with the same bug, there are no suggested fixes... WTF is this, suddenly the values are read as types???
answered Feb 9, 2023 by anonymous

edited Mar 5, 2025
0 votes
In this case I guess this means that since value is either "a" or "b" and the first validation checks if value is not "a", it means that it will enter the first block of code when it's "b", so it will never enter the second block. I guess you meant to write if (value === a){something } else {something else}.
answered Oct 26, 2023 by Nat

edited Mar 5, 2025

Related Questions In TypeSript

0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
1,366 views
0 votes
1 answer

What do square brackets mean in a type definition in Typescript?

I've missed a basic type of TypeScript: Tuples. So ...READ MORE

answered Jun 22, 2022 in TypeSript by Nina
• 3,060 points
4,710 views
0 votes
0 answers

What is TypeScript and why would I use it in place of JavaScript?

Can you please describe what the TypeScript ...READ MORE

Jul 18, 2022 in TypeSript by Logan
• 2,140