Skip to content
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

List transitive failures on failure of oneOfType #398

Open
erikerikson opened this issue Jun 1, 2023 · 0 comments
Open

List transitive failures on failure of oneOfType #398

erikerikson opened this issue Jun 1, 2023 · 0 comments

Comments

@erikerikson
Copy link

using "prop-types": "15.8.1"

Previously discussed at facebook/react#8901, #226, and (sort of) #9. (there are probably some I've missed)

I am opening this to suggest that the message produced during a failure against PropTypes.oneOfType should list the transitive failures for each failing type. If it had been, it would have saved me time and fiddling. I would guess other users would also benefit.

I have a component with prop data that can be a string, an object (of type A), or an object with attributes including an array of objects [of type A]). This depends on the use case I am employing the component in, clearly. Error and code follows.

Error:

Warning: Failed prop type: Invalid prop `data` supplied to `<component>`, expected one of type [string, string].

(in fact, there were three types, the second and third were object types /shrug)

Code:

const A = PropTypes.objectOf(
  PropTypes.shape({
    attrA0: PropTypes.number.isRequired,
    attr...: PropTypes.number.isRequired,
    attrAN: PropTypes.number.isRequired,
  }),
)
const O = PropTypes.shape({
  attrO0: PropTypes.string.isRequired,
  attrO1: PropTypes.arrayOf(A).isRequired,
  attrO2: PropTypes.number.isRequired,
})
Report.propTypes = {
  data: PropTypes.oneOfType([
    PropTypes.string,
    A,
    O,
  ]).isRequired,

I was silly and made some mental mapping mistakes (I needed to add PropTypes.objectOf(...) to A) as well as forgetting an attribute's type s/string/number/ for attrO2. However, while it would have been trivial to figure out the problems given an array of failure messages, being new to React I was just confused for a while, poking around in the dark, until I isolated the cases where each type came in and changed things to debug those:

const A = PropTypes.objectOf(
  PropTypes.shape({
    attrA0: PropTypes.number.isRequired,
    attr...: PropTypes.number.isRequired,
    attrAN: PropTypes.number.isRequired,
  }),
)
const O = PropTypes.shape({
  attrO0: PropTypes.string.isRequired,
  attrO1: PropTypes.arrayOf(A).isRequired,
  attrO2: PropTypes.number.isRequired,
})
Report.propTypes = {
  data: PropTypes.string.isRequired,
  // data: PropTypes.oneOfType([
  //   PropTypes.string,
  //   A,
  //   O,
  // ]).isRequired,
}

then...

Report.propTypes = {
  data: A.isRequired,
  // data: PropTypes.oneOfType([
  //   PropTypes.string,
  //   A,
  //   O,
  // ]).isRequired,
}

then...

Report.propTypes = {
  data: O.isRequired,
  // data: PropTypes.oneOfType([
  //   PropTypes.string,
  //   A,
  //   O,
  // ]).isRequired,
}

Doing that made my errors obvious. Choosing to do that was not immediately obvious due to my inability to understand and reason back from the warning I was seeing. I believe this was exacerbated by the swallowing of the individual errors.

It would have taken a fraction of the time to fix my failures if, in the case a oneOfType fails, the failure for every evaluated type was listed. Even (or especially) if it were 100 types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant