Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant