facebook / prop-types

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

Is there any way I can get type and if the prop is required from prop-types package? #247

Closed daiict218 closed 5 years ago

daiict218 commented 5 years ago

I have a React Component whose prop-types I've defined. Whenever I'm logging out the propTypes in React. it logs me out a function which is possibly the validator. I want to print the propType and whether the prop is required or not (e.g. prop1: {required: true, value: 5}) at this point. How can I do that?

class Component extends React.Component {
   constructor(){
     console.log(Component.propTypes.prop1);
   }
}

Component.propTypes = {
    prop1: PropTypes.number.isRequired,
    prop2: PropTypes.string
}
ljharb commented 5 years ago

Related to #60.

There's no metadata exposed currently, so there's no way to do that.