facebook / prop-types

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

[Help wanted] How to check prop-types spelling? #282

Closed xiaoquisme closed 5 years ago

xiaoquisme commented 5 years ago

Details: I have one react component some like this

class SomeComponent extends React.Component {
   render() {
     return <div>
       {this.props.name}
     </div>
   }
}
SomeComponent.PropTypes = {
  name: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
}

This is correct.

case 1: but some of the Team members missing write PropTypes.

It's ok, Eslint will tell him. case 2: some of the Team members will be writing like this

SomeComponent.PropTypes = {
  name: PropTypes.oneOfType(PropTypes.element, PropTypes.string), 
 // missing **oneOfType** needed array
}

case 3: some of the Team Members will be writing like this

SomeComponent.PropTypes = {
  name:  PropTypes.Hahaha
}

But for Case 2 and 3, Eslint will not tell me this is error and Proptypes not tell me the error in my IDE(such as WebStorm). so how to avoid this?

ljharb commented 5 years ago

you’ll get a runtime error; the best practice imo is to make console warnings/errors fail your tests, and then your component’s tests should catch these.

xiaoquisme commented 5 years ago

Yes, a runtime error will always be ignored, because it does not affect the correct behaver. So the only way is to write a test to cover this. It's correct?

ljharb commented 5 years ago

Yes.

xiaoquisme commented 5 years ago

Thanks a lot