facebook / prop-types

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

New validation: isDeprecated #295

Closed mnm364 closed 5 years ago

mnm364 commented 5 years ago

Do you want to request a feature or report a bug? Request a feature

What is the current behavior? N/A

What is the expected behavior? N/A

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? N/A

I find myself in a position of inheriting a few React components that have wide usage within the organization I work in. I would like to safely change the name of a few prop types and do so in an incremental manner.

To that end, would it be possible to support an isDeprecated check that can be chained alongside other type checks to throw warnings to users of the deprecated props? For example, consider the propTypes

MyField.propTypes = {
    id: PropTypes.number.isRequired,
    name: PropTypes.string,
    value: PropTypes.string,
}

That we want to migrate to

MyField.propTypes = {
    id: PropTypes.number.isRequired,
    displayName: PropTypes.string,
    value: PropTypes.string,
}

To advertise this so as to incrementally allow users of this component to migrate independently of the change, I would like to do something like the following,

MyField.propTypes = {
    id: PropTypes.number.isRequired,
    name: PropTypes.string.isDeprecated,
    displayName: PropTypes.string,
    value: PropTypes.string,
}

Such that supplying the name prop would always generate a warning.

I know I can write my own custom validator -- which is great! -- but I would think others would find this useful as builtin library functionality as well.

If given the green light, I would be happy to take a look into submitting a PR for this.

ljharb commented 5 years ago

PropType warnings fail tests, when tests are set up properly. Adding .isDeprecated here would constitute the same as restrictedProp from https://npmjs.com/airbnb-prop-types - a breaking change, disallowing the prop entirely.

mnm364 commented 5 years ago

Hmmm, that is a fair point and thanks for the quick repy! Is there no conceivable way to allow rules to pass tests but still warn in production? Out of curiosity, do you know how airbnb-prop-types handles this in their testing suite?

ljharb commented 5 years ago

No, no reliable way i can think of, since all propTypes warnings go out the same channel.

There are no hard deprecations internally; the prop usually has a replacement ship as a minor, then a codemod is done, and then the old one is removed as a major.

junpos commented 5 years ago

@mnm364 you might want to check this library, https://github.com/thefrontside/deprecated-prop-type, for the false-positive on props.

mnm364 commented 5 years ago

Woot! Okay, thanks guys for all the input. Appreciate it and your time. Will close this issue out.