React: Rename PropTypes.func to Proptypes.function and deprecate PropTypes.func

Created on 8 Feb 2017  路  6Comments  路  Source: facebook/react

Do you want to request a feature or report a bug?
Bug. Inconsistent naming. Leads to poor user experience for developers.

What is the current behavior?
Using PropTypes.function in the propTypes object for a React class produces a warning or error, "Cannot read property 'isRequired' of undefined. You must use PropTypes.Func.

Every time I'm typing PropTypes, I have to look up whether it's 'func' or 'function,' and sometimes that makes me wonder if it's 'object' or 'obj' too. None of the other PropTypes names are truncated. This is a poor user experience.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/reactjs/69z2wepo/).
MyReactClass.propTypes = {
someFunction: PropTypes.function.isRequired
};

What is the expected behavior?
The above works with no errors or warnings.

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

Most helpful comment

The rule of thumb is that function is a reserved name in JavaScript so we avoid using it in the API as identifier. object is not, so it's safe to use. Does this help? Using function wouldn't be valid in some contexts (e.g. destructuring) which is similarly surprising. Perhaps we could offer a warning in development though (do you want to send a PR?)

We are unlikely to make vast changes to PropTypes at this point. They're mostly in maintenance mode as we encourage people to use static typing (Flow or TypeScript).

All 6 comments

The rule of thumb is that function is a reserved name in JavaScript so we avoid using it in the API as identifier. object is not, so it's safe to use. Does this help? Using function wouldn't be valid in some contexts (e.g. destructuring) which is similarly surprising. Perhaps we could offer a warning in development though (do you want to send a PR?)

We are unlikely to make vast changes to PropTypes at this point. They're mostly in maintenance mode as we encourage people to use static typing (Flow or TypeScript).

Thanks for the detailed and understanding response. Yes, that helps! I hadn't thought about function being a reserved word.

I wasn't aware that you encourage people to use static typing. Would you be able to point me to more info about that?

Does this help?
https://flowtype.org/docs/react.html#defining-components-as-reactcomponent-subclasses

They're mostly in maintenance mode as we encourage people to use static typing (Flow or TypeScript).

@gaearon we've said this for awhile but haven't really been consistent about it. We have a number of PRs open that extend/affect PropTypes in someway. Is there anyway we can be more specific about what things we do and do not want to do with PropTypes right now?

PropTypes have been removed from React core and now exist as a separate package prop-types. Any future feature requests, bug reports, or changes should be directed to the new repo at https://github.com/reactjs/prop-types.

I've opened a new issue at https://github.com/reactjs/prop-types/issues/8 for anyone wanting to continue this discussion.

Was this page helpful?
0 / 5 - 0 ratings