facebook / prop-types

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

Condition is always false since types <optionalShape> and 'null' have no overlap #283

Closed anddero closed 2 years ago

anddero commented 5 years ago

If I want to conditionally render some component, I could use the following pattern:

function MyOptionalComponent(props) {
    if (props.optionalValue === null) return null;

    // otherwise return something rendered from that optionalValue object
    ...
}

If I provide the proptypes for this component:

MyOptionalComponent.propTypes = {
    optionalValue: PropTypes.shape({
        field: PropTypes.string,
        anotherField: PropTypes.number
    })
};

I will always get a warning at the null-check of the component function:

Condition is always false since types '{field: String, anotherField: Number' and 'null' have no overlap

I did not make optionalValue required, therefore it should be optional, therefore there should be no such warning.

anddero commented 5 years ago
"prop-types": "15.7.2",
"react": "16.8.6"
ljharb commented 5 years ago

This warning seems like it might be coming from flow, since propTypes don’t have a warning of this kind.