-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Improve soundness of ReactDOMFiberInput typings #13367
Improve soundness of ReactDOMFiberInput typings #13367
Conversation
This is an attempt in improving the soundness for the safe value cast that was added in facebook#11741. We want this to avoid situations like [this one](facebook#13362 (comment)) where we need to remember why we have certain type casts. Additionally we can be sure that we only cast safe values to string. The problem was `getSafeValue()`. It used the (deprecated) `*` type to infer the type which resulted in a passing-through of the implicit `any` of the props `Object`. So `getSafeValue()` was effectively returning `any`. Once I fixed this, I found out that Flow does not allow concatenating all possible types to a string (e.g `"" + false` fails in Flow). To fix this as well, I've opted into making the SafeValue type opaque and added a function that can be used to get the string value. This is sound because we know that SafeValue is already checked. I've verified that the interim function is inlined by the compiler and also looked at a diff of the compiled react-dom bundles to see if I've regressed anything. Seems like we're good.
b2b714b
to
3e2f19e
Compare
ReactDOM: size: 0.0%, gzip: -0.0% Details of bundled changes.Comparing: 725e499...db84bdc react-dom
Generated by 🚫 dangerJS |
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 is a great change, 👍
@@ -174,13 +177,14 @@ export function updateWrapper(element: Element, props: Object) { | |||
if (props.type === 'number') { | |||
if ( | |||
(value === 0 && node.value === '') || | |||
// We explicitly want to coerce to number here if possible. |
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.
👍
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.
Lets make sure we coordinate this with #13362 so that all our input wrappers are consistent.
ReactDOMFiberSelect
also uses _wrapperState
. Consider using SafeValue
there too for consistency?
@raunofreiberg Awesome! Let's merge then, your PR should be fine if you already have that change 👍 |
Nit: “safe” has security connotations. We want to be careful with naming like this because the reader might think it implies some particular type of safety (for example safety for emitting into HTML without escaping it). |
@gaearon Good point. Do you have another idea? Maybe ConcatenateableValue? Feels a bit long and might lead to typos. Maybe |
|
Maybe |
Following up on the changes I made in facebook#13367, @gaearon suggest that "safe" could be read as necessary for security. To avoid misleading a reader, I'm changing the name. A few names where discussed in the previous PR. I think `ToStringValue` makes sense since the value itself is not a string yet but an opaque type that can be cast to a string. For the actual string concatenation, I've used `toString` now to avoid confusion: `toStringValueToString` is super weird and it's namespaced anyhow. Definitely open for suggestions here. :) I'll wait until we wrap up facebook#13362 and take care of rebase afterwards.
Following up on the changes I made in #13367, @gaearon suggest that "safe" could be read as necessary for security. To avoid misleading a reader, I'm changing the name. A few names where discussed in the previous PR. I think `ToStringValue` makes sense since the value itself is not a string yet but an opaque type that can be cast to a string. For the actual string concatenation, I've used `toString` now to avoid confusion: `toStringValueToString` is super weird and it's namespaced anyhow. Definitely open for suggestions here. :) I'll wait until we wrap up #13362 and take care of rebase afterwards.
This is an attempt in improving the soundness for the safe value cast that was added in #11741. We want this to avoid situations like this one where we need to remember why we have certain type casts. Additionally we can be sure that we only cast safe values to string.
The problem was
getSafeValue()
. It used the (deprecated)*
type to infer the type which resulted in a passing-through of the implicitany
of the propsObject
. SogetSafeValue()
was effectively returningany
.Once I fixed this, I found out that Flow does not allow concatenating all possible types to a string (e.g
"" + false
fails in Flow). To fix this as well, I've opted into making the SafeValue type opaque and added a function that can be used to get the string value. This is sound because we know that SafeValue is already checked.I've verified that the interim function is inlined by the compiler and also looked at a diff of the compiled react-dom bundles to see if I've regressed anything. Seems like we're good.