TypeScript Reference ยท Error Codes Worth Knowing
TypeScript error TS2367: comparison appears unintentional
TS2367 means TypeScript found a === or !== comparison between types with no overlap. Learn what triggers it, what it catches, and how to fix real causes.
TS2367 reads This comparison appears to be unintentional because the types 'A' and 'B' have no overlap. The compiler reports it for ===, !==, == and != when no value could ever be equal on both sides. The comparison would always be false (or always true for !==), which almost always means a typo, a stale union, or a wrong variable.
The check is on type overlap, not on values. Comparing "a" | "b" with "c" is flagged; comparing string with "c" is fine because a string could be "c".
type Mode = "light" | "dark"; function theme(m: Mode) { return m === "auto"; // error TS2367 }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
The error needs no syntax of its own. Typical triggers:
| Code | Why it has no overlap |
|---|---|
x === "c" where x: "a" | "b" |
The literal is outside the union. |
n === "5" where n: number |
Number against string. |
value === undefined after narrowing removed undefined |
Types already excluded it. |
status === 404 where status: "ok" | "error" |
Wrong kind of value. |
a === b where one is a different object type |
Unrelated structures. |
Usage notes
- Read it as a bug report first. Fix the union, the literal, or the variable. Silencing it comes last.
- Narrowing can cause it. After an
if (x === "a") return;the type ofxloses"a", so a laterx === "a"is flagged. That is the compiler showing dead code. - Switch cases are checked too, but a
case "c":label on a"a" | "b"selector reports TS2678 ("Type ... is not comparable to type ..."), with the same meaning and the same fixes. - Comparison with
null/undefinedis allowed for any type understrictNullChecks, so defensive checks do not trigger it. - Escape hatch: compare through a wider type,
(m as string) === "c", only for real boundary code. Prefer narrowing and exhaustive checks.