-
Notifications
You must be signed in to change notification settings - Fork 4
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
FEATURE: Support objects that implement __toString in string propType #14
base: master
Are you sure you want to change the base?
FEATURE: Support objects that implement __toString in string propType #14
Conversation
I am not entirely sure about this but this would at least resolve the problems that translation tokens are rejected from the string propType unless translate() is called explicitly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure on this one either. At first glance, this makes perfect sense. But there's a problem on the PHP side of things.
PHP 8 does elaborate on this concept by introducing the Stringable
interface: https://www.php.net/manual/en/class.stringable.php
Yet, even in PHP 8 string
and Stringable
are not the same thing in a declare(strict_types=1);
environment and would need to be explicitly typehinted via union: string|Stringable
.
The suggested design of PropTypes.string
would lead to a false contract by suggesting that the validated prop could be passed to Eel-Helper methods with a string
parameter TypeHint. But all values that are actually just objects implementing __toString
won't pass that TypeHint.
I have two suggestions to remedy this:
-
In PHP 8, if you use a
string|Stringable
TypeHint on a method signature, the parameter will implicitly be converted to a string. That's a pretty weird design choice, that I do not really understand. But, since PHP is doing it this way, it might be worth considering thatPropTypes
does the same. It would be possible, but it would also necessitate thatPropTypes
are available inProduction
contexts - so I'd consider it a bad option after all. -
Introduce
PropTypes.stringOrStringable
. This might be cumbersome to write on the consumer's end and would probably cause a bit of confusion over when it should be preferred overPropTypes.string
. But contract-wise it would suggest the correct ideas:
- This value will be printed and concatenated correctly as a string
- Better use
String.toString
before passing it to an Eel-Helper method
Wdyt?
@grebaldi not sure wether we should model propTypes after php as this is "just" the language it is written in. I would rather reflect about wether or not we consider it ok from a fusion / component perspective. I kind of find it annoying that code that does exactly what it is supposed to do in production is currently rejected in development. Let us think about what can go wrong for a minute: The only case i think this might lead to errors is when components accept string proptypes but later use object methods. This is a bit exotic to me and i am not sure i want to have extra code for that. |
To keep things simple, I would appreciate the simple solution validate The case, that someone defines a prop to be a string and later use is as object is kind of a wrong definition. If the object ist used later on, the corresponding type should be used instead of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pull request works without errors
The problem isn't so much the prop being used as an object later on. It's more that Imagine you define a prop as Admittedly, most built-in Eel-Helpers aren't that strict and most string-related methods even try to do an implicit conversion. But there's other trouble awaiting even with the built-ins, like:
These are just examples off the top of my head - I'm sure there's even more problems that are not quite forseeable right now. Theoretically, we could just document that I'm sorry, but I have to reiterate my objection :( It'd be best to keep the behavior of |
@grebaldi I got you. So a way to handle this will be to add a new Proptype. I would suggest Additionally the exception for the This as @mficzel said, would solve the Wdyt? |
This change replaces the default flow string validator that only accepts null or strings with a custom one that will also accept objects that implement __toString.
Among other things this will be allowed by this:
Resolves. #13