This comparison appears to be a mistake
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.
- 1Comparing a 'string' to a 'number'.
- 2Checking if an object is equal to a primitive value.
- 3Comparing two distinct, unrelated types.
Comparing a variable of type 'string' with a 'number'.
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.
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.
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.
✕ 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.
microsoft/TypeScript src/compiler/diagnosticMessages.json
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev