-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce the "comparable" relation #5517
Conversation
@@ -888,6 +888,10 @@ | |||
"category": "Error", | |||
"code": 2322 | |||
}, | |||
"Type '{0}' is not comparable with type '{1}'.": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer 'comparable to' here. A little research shows that grammarians think they're about equally common, while Google's hideously unreliable hit count shows 4 times more hits for 'comparable to'*.
But the function is also named isTypeComparableTo
, so I think there's precedent. :)
*Google hit counts are the laziest way to do quantitative linguistics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I couldn't decide, but I'm okay with either.
@@ -8,7 +8,7 @@ tests/cases/compiler/literals-negative.ts(5,9): error TS2352: Neither type 'numb | |||
var s = <string>(null); | |||
var b = <boolean>(n); | |||
~~~~~~~~~~~~ | |||
!!! error TS2352: Neither type 'number' nor type 'boolean' is assignable to the other. | |||
!!! error TS2323: Type 'number' is not comparable to type 'boolean'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"comparable" is an odd error to issue when complaining about a type assertion. Can we say "x cannot be converted to y" with the "comparable to" error as the next diagnostic chain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Conflicts: src/compiler/checker.ts
@DanielRosenwasser can we close this. |
I'm planning to merge this in for the tests as a heads up. |
This pull request introduces a new relationship to check whether or not two types are "comparable to" one another. Specifically, it addresses the issues found in #5300 with union types and certain operations.
The new relationship generally subsumes the uses for assignability where we would check whether or not two types were assignable to or assignable from one another. These uses were in checking for compatibility between
===
,!==
,==
,!=
).case
clause expressions and their respectiveswitch
expressions.Like with other type relations, the comparable relation takes a source and a target. It is the most lenient relation, so types that are assignable are always comparable, but the reverse is not necessarily true. The comparable relation differs from the assignability relation in that if the source is a union type, the relation succeeds provided that any of its constituents are comparable to the target.
Keywords: comparability