TS2367
TypeScriptERRORCriticalType SystemMEDIUM confidence

This comparison appears to be a mistake

What this means

A comparison (e.g., '==', '===', '>') is being made between two types that have no overlap, resulting in a condition that will always be true or false.

Why it happens
  1. 1Comparing a 'string' to a 'number'.
  2. 2Checking if an object is equal to a primitive value.
  3. 3Comparing two distinct, unrelated types.
How to reproduce

Comparing a variable of type 'string' with a 'number'.

trigger — this will error
trigger — this will error
let value: string = "5";
if (value === 5) {
  console.log("This will never happen");
}

expected output

error TS2367: This comparison appears to be a mistake because the types 'string' and 'number' have no overlap.

Fix 1

Convert one of the types

WHEN The values are comparable but have different types.

Convert one of the types
let value: string = "5";
if (parseInt(value, 10) === 5) {
  console.log("This can happen now");
}

Why this works

Type conversion allows for a meaningful comparison between the values.

Fix 2

Fix the variable's type or value

WHEN The data is incorrect.

Fix the variable's type or value
let value: number = 5;
if (value === 5) {
  console.log("This will happen");
}

Why this works

Ensuring that both operands in the comparison have compatible types.

What not to do

Use '==' instead of '===' to allow type coercion

While it might hide the error, loose equality has complex coercion rules that can lead to unexpected and buggy behavior.

Sources
Official documentation ↗

microsoft/TypeScript src/compiler/diagnosticMessages.json

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors