facebook / prop-types

Runtime type checking for React props and similar objects
MIT License
4.48k stars 356 forks source link

Trouble chaining oneOf() with isRequired #313

Closed stefcameron closed 4 years ago

stefcameron commented 4 years ago

It appears that there's a chaining issue with PropTypes.oneOf([...]).isRequired, which results in undefined, even though isRequired should be chainable with that type, based on my (very limited) understanding of the code here: https://github.com/facebook/prop-types/blob/master/factoryWithTypeCheckers.js#L341

When I define

Component.propTypes = {
  myProp: PropTypes.oneOf([1, 2, 3]).isRequired
};

I get this error in the browser console:

checkPropTypes.js?c42a:20 Warning: Failed prop type: Component: prop type `myProp` is invalid; it must be a function, usually from the `prop-types` package, but received `undefined`.

I'm seeing this in prop-types@15.7.2

Is isRequired really not chainable in this case?

ljharb commented 4 years ago

It surely should be chainable, and as you can see, it is. Are you certain you haven't mocked out prop-types somehow?

stefcameron commented 4 years ago

@ljharb Thank you for quick the reality check! Terribly sorry. I had a prop type defined in one file and referenced in another where the other file was adding .isRequired to the definition, so when I added .isRequired to the source, that resulted in undefined in the other file. 🤦‍♂