facebook / prop-types

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

instanceOf should follow prototype chain? #195

Closed wmertens closed 5 years ago

wmertens commented 6 years ago

It can happen that some prop is actually an instance of a subclass of what is expected by instanceOf. For example, with HMR, some classes are wrapped.

For these cases it would be nice if the prototype chain is followed.

I see two possibilities:

  1. adjust instanceOf to check Object.getPrototypeOf(value) recursively
  2. add another predicate for this case

The nice thing about 1. is that it will fix spurious warnings when using HMR.

ljharb commented 6 years ago

Can you provide a PR that would demonstrate how 1 would work in concert with HMR?

ljharb commented 5 years ago

instanceOfSubclass instanceof ParentClass is already true; I'm not sure what needs to change here.

The issues with HMR are that it breaks object identity, and it can't be fixed except in the HMR tool itself.