-
Notifications
You must be signed in to change notification settings - Fork 157
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
Add emu-note about why valueOf
throws
#1681
Comments
Babel handles this properly with the strict transpilation, but not with the loose one. This is a transpiler bug when present; it’s fixable by them and the proposal shouldn’t be concerned with it (beyond filing issues etc) |
@ljharb what is the "correct" transpilation behavior in this case? |
Wrapping it in String(), effectively - iirc Babel actually uses the String concat method to ensure proper spec semantics. |
There's an open issue for TypeScript about this transpilation behaviour at microsoft/TypeScript#39744. I've added a comment about the |
Speaking of the spec, would it be possible to elaborate on the spec why these objects throw in their valueOf methods? https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof isn't particularly informative. |
Meeting, August 5: the reasoning for valueOf throwing might be a good thing to add in an |
FYI, I opened a PR against React to fix the problem there. I'm starting to worry that this behavior will cause a lot of problems in the wild, given how often we've seen it so far with mainstream platforms. If we decided to not throw in valueOf, what bad things would happen? |
The discussion is in #517. (tl;dr: if you include a valueOf, then people will use The new angle here is actually not that people are using comparison operators, but that they are (indirectly, via a library or a transpiler) using the In the thread in #517 there was another suggestion: having valueOf return a primitive type (likely a BigInt) such that the comparison operators always worked. The disadvantage of this was that cross-type comparisons, e.g. (For example, with the TypeScript bug, you'd get something like this:) date = new Temporal.PlainDate(1970, 1, 1);
console.log(`The date is ${date}`);
// JavaScript: The date is 1970-01-01
// TypeScript: The date is 0 |
Another detail to add: one of the reasons for |
I would not hold my breath for that to work out smoothly. |
I think this can be marked as fixed given that both Babel and TypeScript now handle this correctly - TS is fixed as of v4.5. |
I think there was still a pending action to add an |
valueOf
, which will throwvalueOf
throws
These notes clarify in the spec text why valueOf() must throw, which is a request we've had. It also suggests what implementations might put in the error message to point users in the right direction. Closes: #1681
js-temporal/temporal-polyfill#21 (comment) exposed an issue we should look at. The problem @12wrigja found is that when string interpolation is polyfilled by TS (and perhaps by Babel too?) it's converted to
+
, which per https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof will throw.This implies that code using Temporal instances in string interpolations will work fine in native ES6 but will throw when polyfilled into ES5. Is this OK? If not, are there any ways around it (e.g. by using
@@toPrimitive
) ?The text was updated successfully, but these errors were encountered: