question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`any` proptype should not be marked as required

See original GitHub issue

In the following example, foo probably should not be marked as required:

type Props = {
  foo: any,
}

const MyComp = (props: Props) => <div />

<MyComp foo={null} />
// Failed prop type: The prop `foo` is marked as required in `MyComp`, but its value is `null`.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
marcindcommented, Nov 15, 2017

@echenley did not run the updated code but looking at the test case coverage, this seems like it should work as I would expected

type Props {
   foo?: any
}
const Comp = (props: Props) => null;

to result in

const Comp = (props) => null;
Comp.propTypes = {
   foo: require('prop-types').any,
}

i.e. the behavior before the first PR

So :shipit:

0reactions
brigandcommented, Nov 16, 2017

For completeness of this thread, this is the new test echenley added. It handles both cases properly.

type Props = {
  requiredAny: any,
  optionalAny?: any,
};
const Foo = (props: Props) => <div />
Foo.propTypes = {
  requiredAny: function requiredAny(props, propName, componentName) {
    if (!Object.prototype.hasOwnProperty.call(props, propName)) {
      throw new Error('Prop \`' + propName + '\` has type \\\\'any\\\\' or \\\\'mixed\\\\', but was not provided to \`' + componentName + '\`. Pass undefined or any other value.');
    }
  },
  optionalAny: require('prop-types').any
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

React - prop-types is marked as required - Stack Overflow
You're trying to load the <Rate /> component with a rate prop equal to this.state.rate in <App /> . Your App component state...
Read more >
Don't Call PropTypes Warning - React
Don't call PropTypes directly​​ Using PropTypes in any other way than annotating React components with them is no longer supported: var apiShape =...
Read more >
How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >
Warning for: PropTypes.object.isRequired when prop is `null ...
Requiring that a value or null must be specifically provided should absolutely be allowed via PropTypes. Here is the use case I'm running...
Read more >
PropTypes.shape should support warn on missing required ...
My feature request was to have some warning when the object passed to a property can be statically analyzed, and is missing some...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found