-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
PropTypes: distinguish nullable from optional object field #7291
Conversation
cc @spicyj @keyanzhang realized this confusion during the codemod. |
null, | ||
ReactPropTypesSecret | ||
); | ||
expect(error2 instanceof Error).toBe(true); |
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.
Nit but I guess you’d want to do the instanceof
check for error1
too?
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.
Nice catch, corrected, thanks.
To be clear, this distinguishes null from undefined, but does not distinguish undefined from an absent field – right? |
Right, I don't tackle that. |
@spicyj is this good for merging? |
@@ -144,6 +144,12 @@ function createChainableTypeChecker(validate) { | |||
if (props[propName] == null) { | |||
var locationName = ReactPropTypeLocationNames[location]; | |||
if (isRequired) { | |||
if (props[propName] === null) { | |||
return new Error( | |||
`Required ${locationName} \`${propFullName}\` was specified in ` + |
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.
Can we do
The ${locationName} `${propFullName}` is required in ${componentName}, but its value is null.
and replace the other message with "…its value is undefined."?
lgtm. Can you send me a diff internally to html/shared/core/createWarning.js
adding the new wording so we don't forget?
I'd like the wording to be clearer and more accurate as noted inline, but after you fix that feel free to squash and merge. |
This gives a more precise message (no type semantics change) to the case of passing a field in an object, but whose value is `null`: Before: ```js propTypes: { foo: React.PropTypes.number.isRequired } ``` Would scream "Required prop `foo` was not specified in `MyComp`". Now it'll be "Required prop `foo` was specified in `MyComp`, but its value is `null`.". Works as expected in nested objects. This fixes the issue of a component transitively passing a `null`, specifying the correct field to the child but have the child tell it that it didn't provide the prop. Optional field and nullable are two different things anyway.
* PropTypes: distinguish nullable from optional object field This gives a more precise message (no type semantics change) to the case of passing a field in an object, but whose value is `null`: Before: ```js propTypes: { foo: React.PropTypes.number.isRequired } ``` Would scream "Required prop `foo` was not specified in `MyComp`". Now it'll be "Required prop `foo` was specified in `MyComp`, but its value is `null`.". Works as expected in nested objects. This fixes the issue of a component transitively passing a `null`, specifying the correct field to the child but have the child tell it that it didn't provide the prop. Optional field and nullable are two different things anyway. * Add missing test case. * Reword messages. (cherry picked from commit 0292d34)
This gives a more precise message (no type semantics change) to the case of passing a field in an object, but whose value is
null
:Before:
Would scream "Required prop
foo
was not specified inMyComp
".Now it'll be "Required prop
foo
was specified inMyComp
, but its value isnull
.".Works as expected in nested objects.
This fixes the issue of a component transitively passing a
null
, specifying the correct field to the child but have the child tell it that it didn't provide the prop.Optional field and nullable are two different things anyway.