Skip to content

Comparison for different/incompatible types errors on equality, but not on greater/less comparisons #46157

@gyzerok

Description

@gyzerok

Bug Report

🔎 Search Terms

is:issue is:open incompatible types comparison

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about common bugs, tried searching for equality and comparisons.

⏯ Playground Link

Playground link with relevant code

💻 Code

type Opaque<Type, Token = unknown> = Type & {
  readonly __opaque__: Token
}

type One = Opaque<number, 'One'>
type Two = Opaque<number, 'Two'>

const one: One = 1 as One
const two: Two = 2 as Two

console.log(one === two) // error - incompatible types
console.log(one == two) // error - incompatible types
console.log(one < two) // works? why?

🙁 Actual behavior

The current behavior feels unexpected to me. I would understand that this is due to JavaScript compatibility if equality checks would succeed.

Also in other programming languages that I know it's usually so that greater/less comparisons can be applied only to subset of types where equality can be applied. So if the errors would be other way around it would make sense for me.

So in my opinion currently the way TypeScript compiler works is confusing.

🙂 Expected behavior

I would expect to either:

  1. See errors everywhere - this is preferred behavior for me
  2. See errors nowhere
  3. < comparison to error, while == and === to compile

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