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
At runtime, each operand of a relational operator is converted to a primitive value if needed by calling its valueOf() method (among other things). A type can be made effectively incomparable by giving it a valueOf method that throws an exception.
Ideally, tsc would check the valueOf signature of each operand, and produce a type error for any relational expression that implicitly invokes a valueOf whose declared return type is never or void.
π Motivating Example
The following code unconditionally throws at runtime and passes type-checking.
classC{valueOf(){thrownewError("Please don't call me again!")}}consta=newC();constb=newC();if(a<b){// <- No errorconsole.log("I am unreachable");}
TC39 Temporal PlainDate and Instant types throw an exception when compared this way (for reasonable reasons). It's easy to accidentally write code that naively compares these types using an operator instead of the appropriate compare method. It would be ideal if this was caught statically.
The text was updated successfully, but these errors were encountered:
But today you can still write wrap(123n) + 456 and you won't get any complaint. If we had this, then we could make that an error, which would be great!
On behalf of the Temporal champions, this proposal sounds great! π
+ is almost certainly doomed to permanent weirdness due to Date, unfortunately.
The type of Date.prototype.valueOf doesn't have a type of never or void, so it wouldn't be affected by this proposal, right?
I agree that Date will be forever weird for + and many other reasons, but this proposal seems like it could be a way to use the type checker to warn users that they are doing something that may fail at runtime. What I particularly like is that it allows the type author to opt into this behavior rather than hard-coding something just for some types like Temporal.ZonedDateTime.
Suggestion
π Search Terms
valueof never
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
At runtime, each operand of a relational operator is converted to a primitive value if needed by calling its
valueOf()
method (among other things). A type can be made effectively incomparable by giving it avalueOf
method that throws an exception.Ideally,
tsc
would check thevalueOf
signature of each operand, and produce a type error for any relational expression that implicitly invokes avalueOf
whose declared return type isnever
orvoid
.π Motivating Example
The following code unconditionally throws at runtime and passes type-checking.
Playground link
π» Use Cases
TC39 Temporal
PlainDate
andInstant
types throw an exception when compared this way (for reasonable reasons). It's easy to accidentally write code that naively compares these types using an operator instead of the appropriatecompare
method. It would be ideal if this was caught statically.The text was updated successfully, but these errors were encountered: