Skip to content

Type 'X' is not comparable to type 'Y'

Error message

                Type is not comparable to type
              

Quick summary

You compared two values whose types have no overlap — the comparison will always be false or true. TypeScript flags this as a likely logic error.

Why this happens

  • Comparing a string to a number with ===

  • Comparing two different literal types

  • Comparing a value to a type it can never be

  • Logic errors in type narrowing

Minimal example

✗ Broken code
const x: string = "hello";
if (x === 42) { // always false: string vs number
  console.log("never reached");
}
✓ Fixed code
const x: string | number = "hello";
if (x === 42) {
  console.log("x is 42");
}

Widen the type to include both string and number, or remove the impossible comparison.

How to diagnose

  • Check the types of both sides of the comparison

  • Determine if the comparison is logically meaningful

  • Look for type narrowing that may have made the comparison unnecessary

How to fix

  • Change the comparison to use compatible types

  • Remove the comparison if it's always true/false

  • Use a type assertion if the types are actually compatible at runtime

  • Fix the type annotations that led to the impossible comparison

How to prevent

  • Use strict equality (===) to catch type mismatches

  • Enable strict mode for better type checking

Related Resources

Related Glossary

← Back to language