ljharb / prop-types-tools

Custom React PropType validators
MIT License
671 stars 50 forks source link

Throw if called directly? #16

Closed lencioni closed 7 years ago

lencioni commented 7 years ago

The new prop-types package throws if the prop types are called directly.

Should we do something similar here for consistency and safety? (Is this already happening and I'm just blissfully unaware?)

ljharb commented 7 years ago

No, we definitely shouldn't.

React is doing that so it can be free to stop including PropTypes in production. We don't have that concern as someone can simply alias our package to nothing, or require the main entry point.

I'm not sure why the prop-types package needs to throw if called directly; it shouldn't be any of React's business how someone chooses to use that package. @gaearon, any insight?

lencioni commented 7 years ago

My assumption was that they are throwing to help protect people from relying on some behavior that will be broken in production because the functions are converted to noops.

ljharb commented 7 years ago

Right, that makes sense with their added production compilation; but since we have both a direct import and an environment-specific one, and it's clearly documented (which is the approach I suggested to @sebmarkbage that React use) the throwing behavior doesn't make sense to me.

gaearon commented 7 years ago

I can conceivably imagine people using our PropTypes for, e.g., form validation. So it seemed that we have to be very explicit about this behavior change in our package.