Ad Space — Top Banner

TS2367

TypeScript Programming Language

Severity: Minor

What Does This Error Mean?

TypeScript can see that your comparison will always be false. The two types you are comparing have no values in common. Either your logic has a bug, or the types need to be corrected.

Affected Models

  • TypeScript 5.x
  • TypeScript 4.x
  • React + TypeScript
  • Node.js + TypeScript

Common Causes

  • Comparing a string variable to a number literal — they can never be equal
  • Comparing two different literal types that can never match
  • A type narrowing mistake where the type has already been narrowed away by a previous check
  • Checking a boolean with === true when TypeScript already knows it is true
  • Using the wrong variable in a comparison — a copy-paste error

How to Fix It

  1. Read the error carefully — TypeScript tells you both types and confirms they cannot overlap.

    Example: 'This condition always returns false because string and number have no overlap.'

  2. Check if you are comparing the right variables — this is often a copy-paste bug.

    Make sure both sides of === are the values you intended to compare.

  3. If comparing a union type to a literal, make sure the literal is one of the union's members.

    If status: 'active' | 'inactive' and you check status === 'pending', TypeScript correctly warns it is always false.

  4. Remove dead code that can never execute due to the impossible comparison.

    If the condition is always false, the code inside the if block never runs. Remove or rethink the logic.

  5. Fix the type definition if the comparison should be valid but the types are too narrow.

    If you expect more values to be possible, expand the type definition to include them.

When to Call a Professional

TS2367 is a logic warning you can always fix yourself. It often reveals a genuine bug — TypeScript spotted dead code that could never run.

Frequently Asked Questions

Is TS2367 an error or just a warning?

It is a TypeScript error that prevents compilation. It treats unreachable comparisons as errors because they almost always indicate a bug. Fix the logic or the types to resolve it.

Why does TypeScript catch this when JavaScript would just run it?

JavaScript evaluates all code at runtime — even code that can never be true. TypeScript analyses the types statically and can detect impossible conditions before the code runs. This saves you from shipping silent bugs.

What if I actually want to compare two values that might have different types at runtime?

Use a type assertion or widen the types so the comparison is valid. Alternatively, convert both sides to a common type before comparing. Example: String(value) === String(otherValue) compares both as strings.