Skip to content
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

Merged
merged 24 commits into from
Mar 30, 2016
Merged

Conversation

DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Nov 4, 2015

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

  • the types of the left and right sides of equality/inequality operators (===, !==, ==, !=).
  • the types of case clause expressions and their respective switch expressions.
  • the type of an expression in a type assertion with the type being asserted.

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

@@ -888,6 +888,10 @@
"category": "Error",
"code": 2322
},
"Type '{0}' is not comparable with type '{1}'.": {
Copy link
Member

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.

Copy link
Member Author

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'.
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2015

@DanielRosenwasser can we close this.

@DanielRosenwasser
Copy link
Member Author

I'm planning to merge this in for the tests as a heads up.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants