Proposal: Introduce a Relational Comparison Type Relationship #10120
Labels
Committed
The team has roadmapped this issue
Fixed
A PR has been merged for this issue
Suggestion
An idea for TypeScript
Milestone
Problem
Before literal types and union types, it made sense that relational comparison operators and equality comparison operators had the same rules.
Overly-permissive
However, with union types, relational comparisons don't always make sense.
Here, it's not clear that you'll get the "correct" behavior if
x
andy
are not bothstring
s, but TypeScript permits this. Issue #5156 covers this in more detail.Overly-strict
While the current "comparable" relationship is the most lax relationship, it is also too strict. For instance, consider the following example from #10119:
Under the comparable relationship, TypeScript issues an error because the type
2
is not comparable with1
nor is it comparable with3
.Proposal
I propose a new relational comparison relationship, in which for any types S and T:
To note:
Open Questions
It's not clear how this behaves with type parameters. For instance, you might propose that we just check the type parameter's constraint here, but that's not enough. If you constrain
T
tonumber | string
, thenT
will not be relationally comparable to itself because two distinct values of typeT
could each have different types at runtime. See #5156 (comment) for a concrete example.The text was updated successfully, but these errors were encountered: