-
Notifications
You must be signed in to change notification settings - Fork 153
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
Throwing in valueOf causes errors when date is casting to string #1462
Comments
I don't think that any transpiler or minifier does this, and if they do then it seems like a bug to me. Consider the following code that would also break then: const foo = {
valueOf: () => '1',
toString: () => '2'
}
console.log('' + foo)
console.log(`${foo}`) e.g. Babel correctly transpiles this into: "use strict";
var foo = {
valueOf: function valueOf() {
return '1';
},
toString: function toString() {
return '2';
}
};
console.log('' + foo);
console.log("".concat(foo)); |
Alright, maybe I'm wrong about transpilers and minifiers (I hope so), but this doesn't solve problems with libraries |
It’s incorrect to concat to a string to convert to one, for this exact reason (Date objects are affected too). Code should be using |
@revyh Yes, you're right that libraries may do this. We've seen one instance of this so far with React, #1224 / facebook/react#20594 which was fixed quite promptly. Is it possible to isolate the place in Vue where this is happening and report a bug? |
@ljharb yes, now I understand that. But just recently I thought that concatenation with empty string is just another way to cast something to string and that it's absolutely safe to sum anything with a string. And I assume that many people think the same way despite the fact that it's a misunderstanding. And these people authoring libraries. And by breaking this assumption we potentially break a large number of libraries. @ptomato the issue with vue relates to the code that validates passed props according to propTypes. And I can overcome this issue simply by using more specific propTypes like I guess, that we've seen this problem with only few libraries so far because the Temporal API is not widely used currently. But with the growing adoption of the Temporal API we'll see more bugs like this. As I see, you guys are well aware of these consequences and your decision is conscious. So, feel free to close the issue. Just in case, if someone thought that I'm offended by the unwillingness to make changes then it's not the case. I understand that it was a tough decision and it's unlikely that someone wants to reiterate on this, especially at stage 3. I'm just trying to make the conversation productive. |
@revyh such libraries were already broken by |
It's debatable whether they were broken by |
I think we should close this, but keep an eye on whether more of these bugs pop up in libraries, especially if they can't be fixed. (However, I'm not sure we'd change the fact that |
The issue is that expressions like
'' + Temporal.now.instant()
throw error. I've read through several discussions aboutvalueOf
(namely #517 and #74) and I think I understand the motivation behind the decision to throw errors fromvalueOf
. But at the same time, throwing an error in such a widespread way of casting to string makes Temporal dates very fragile.Of course, you can workaround this by using something like
Temporal.now.instant().toString()
or`${Temporal.now.instant()}`
. But the real problem arises when you want to use Temporal in a code that you don't control. For example, I encountered this problem when I tried to use Temporal as a prop in a Vue component. I assume the same thing can happen with other libraries, especially the older ones. Or even worse, transpilers or minifiers can assume that it's safe to transform`Current date is ${Temporal.now.instant()}`
to'Current date is ' + Temporal.now.instant()
orTemporal.now.instant().toString()
to''+Temporal.now.instant()
. So, your code may work fine in development but break in production.The text was updated successfully, but these errors were encountered: