facebook / prop-types

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

Wrong type of prop reported when using (bool && arrow function) #329

Closed erheron closed 3 years ago

erheron commented 4 years ago

Hi all! I've discovered that PropTypes can't handle a simple case below. I also consider the possibility that I've missed something because I'm new to react-native and PropTypes

Versions

react-native 0.62.2 proptypes 15.7.2

Reproducing example (the clue only)

const ParentComponent = ({onEdit, someArgument}) = >{
   const handleOnEdit = onEdit && ( () => onEdit(someArgument) );
   return(
    <ParentComponent>
        <ChildComponent
           onEdit={handleOnEdit}
        />
    </ParentComponent>);
}

.....
.....
ChildComponent.propTypes = {
    onEdit : PropTypes.func,  // runtime warning: bool supplied (why?)
}
ljharb commented 3 years ago

Can you provide the full stack trace? Since you're using RN, are you perhaps using hermes with it?

datenreisender commented 3 years ago

If onEdit is false that false will be passed in the property onEdit to ChildComponent. Since false is a boolean, that value does not satisfy the prop type func, so the warning is correct.

erheron commented 3 years ago

Thank You @datenreisender , I haven't considered that!