Skip to content

Incorrect diagnostic: comparing objects with == doesn't account for implicit coercion #55305

Closed as not planned
@rotu

Description

@rotu

🔎 Search Terms

Rule 2839 does not seem to know about Javascript's loose equality semantics for objects. It falsely states that:

This condition will always return 'false' since JavaScript compares objects by reference, not value.

Below are some examples that trip this diagnostic even though they evaluate to true.

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 5.20

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.0-beta#code/MYewdgziA2CmB00QHMAUByA2iARgK1mABcACAeX0KIF10SBeekgbwF8BKAWAChRIYESNACIAZiBDCGTZiSIgAykQBOASzDIAXCVTt6APjESpHHnyhxEKVEcnSWJAG4BDaAFdYZUdt0HbJrl5wC0Frf3tZTAUATwBbHBh4eQAFNVjVIlVHWGofPUNxO1Mg-kshVABGCJIouIToJJBU1XTM7NydfKqOIA

💻 Code

'[object Object]' == {}
"foo" == { toString: ()=>"foo" }
"foo" == { valueOf: ()=>"foo" }
"foo" == { [Symbol.toPrimitive]: ()=>"foo" }
1 == { [Symbol.toPrimitive]: ()=>1 }

🙁 Actual behavior

The expressions all evaluate to true, but TypeScript erroneously reports:

This condition will always return 'false' since JavaScript compares objects by reference, not value.

And:

This comparison appears to be unintentional because the types 'string' and '{ toString: () => string; }' have no overlap.

🙂 Expected behavior

These error messages should not be shown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions