facebook / prop-types

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

Ability to verify a prop is a React.Fragment #273

Closed slorber closed 5 years ago

slorber commented 5 years ago

@ljharb suggested here to open a new issue to verify react fragment proptypes: https://github.com/facebook/prop-types/pull/211#issuecomment-491341623

Just to give more context, for react-intl FormattedMessage component, I'd like to submit a PR to add support for fragments.

Currently the lib has tagName: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),

I want to submit a PR to update to something like tagName: PropTypes.oneOfType([PropTypes.string, PropTypes.element, React.Fragment]),

The lib is able to support fragments at runtime. I'm already using tagName=Fragment. I just want a solution to remove the propTypes warning I have when using Fragments.

I've noticed it works with elementType but I guess my PR wouldn't be accepted by react-intl team as it's a bit too "wide" and less specific

kuehlein commented 5 years ago

I'm currently using PropTypes.symbol as a work around to suppress the error. I'm not sure if that is specific enough to avoid any possible edge cases though.

ljharb commented 5 years ago

@slorber PropTypes.oneOfType([PropTypes.string, PropTypes.element, PropTypes.oneOf([React.Fragment])) should do it, no?

slorber commented 5 years ago

Hmmm maybe, didn't try that one ;)

Using TS now so not really using proptypes anymore

ljharb commented 5 years ago

TS isn’t a replacement for PropTypes; it can check number but not integer; it doesn’t have negated types yet; it can’t do ranges, or many other things. I’d strongly suggest using both.

However. I’ll close this, if you no longer have the feature request.