You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should unions and non-primitives be allowed for relational comparisons? #5156
From an offline discussion, the consensus seems to be that the equality operations could keep something like this behavior, whereas other comparison operators should have their own rules.
changed the title [-]Should unions and non-primitives be allowed for relational comparisons[/-][+]Should unions and non-primitives be allowed for relational comparisons?[/+]on Mar 25, 2016
after reading the original bug, maybe what I am posting is slightly different (a regression?) so maybe this should be a separate issue.
edit: hmm, it seems like some object types can do relational operations correctly (such as Moment objects) so maybe I am wrong about this. would like to hear other opinions!
I have been looking into this again - one direction that I've taken has been that two variables of type string | number should not be comparable in a relational manner (i.e. using <, >=, etc.), since each value could be of the other type. I've also gone down the path that a type parameter is only relationally comparable to a type if its constraint is.
One question I ran into is whether this code should be allowd to be wrong:
Now, this function is "bad" in some sense - substituting T with anything other than number or string (and I guess boolean) will have questionable behavior. (Though keep in mind that the function has never been used badly!)
In any case, let's assume that we want to fix this. Let's start writing two overloads:
What can you put in for ???? number | string won't work because then one can be a number and one can be a string. Really what I'd like to say is that if a has a union type, then b is of an appropriate branch of that type for the purposes of comparison.
It's not entirely clear what to do here, but I think "good enough" might be the place to land.
Activity
LPGhatguy commentedon Oct 8, 2015
Comparing strings and numbers is perfectly valid (if not a little wacky) JavaScript, isn't it?
weswigham commentedon Oct 8, 2015
I mean, comparing anything is "valid javascript" (objects, arrays...), if not obviously meaningful.
DanielRosenwasser commentedon Oct 29, 2015
From an offline discussion, the consensus seems to be that the equality operations could keep something like this behavior, whereas other comparison operators should have their own rules.
mhegazy commentedon Feb 20, 2016
We should keep the idea of a new relationship on the backlog, i do not think we solved the underlying issue. closing this to avoid duplication though.
DanielRosenwasser commentedon Mar 9, 2016
This wasn't regarding the comparable relation, and even if it was, we've decided to use it for nullable types.
DanielRosenwasser commentedon Mar 25, 2016
I think that this is a bug and that
>,>=,<, and<=, should probably be separated out from this chart and treated independently.[-]Should union types be comparable?[/-][+]Should unions and non-primitives be allowed for relational comparisons[/+][-]Should unions and non-primitives be allowed for relational comparisons[/-][+]Should unions and non-primitives be allowed for relational comparisons?[/+]jasonswearingen commentedon Mar 25, 2016
why are relational operators valid for non-primitives in typescript?
given that typescript is to make type checking pracical in javascript, I find it remarkable that things like the following do not give syntax errors:
jasonswearingen commentedon Mar 25, 2016
further, why allow relational operations at all for non-primitives as they are useless and indicative of a coding or logic mistake?
jasonswearingen commentedon Mar 25, 2016
after reading the original bug, maybe what I am posting is slightly different (a regression?) so maybe this should be a separate issue.
edit: hmm, it seems like some object types can do relational operations correctly (such as Moment objects) so maybe I am wrong about this. would like to hear other opinions!
DanielRosenwasser commentedon Jul 11, 2016
I have been looking into this again - one direction that I've taken has been that two variables of type
string | numbershould not be comparable in a relational manner (i.e. using<,>=, etc.), since each value could be of the other type. I've also gone down the path that a type parameter is only relationally comparable to a type if its constraint is.One question I ran into is whether this code should be allowd to be wrong:
Now, this function is "bad" in some sense - substituting
Twith anything other thannumberorstring(and I guessboolean) will have questionable behavior. (Though keep in mind that the function has never been used badly!)In any case, let's assume that we want to fix this. Let's start writing two overloads:
Looks decent so far. Now what about an implementation signature?
What can you put in for
????number | stringwon't work because then one can be anumberand one can be astring. Really what I'd like to say is that ifahas a union type, thenbis of an appropriate branch of that type for the purposes of comparison.It's not entirely clear what to do here, but I think "good enough" might be the place to land.
RyanCavanaugh commentedon Apr 24, 2017
Seems we fixed this. Sometime things just work out!