es-shims / es5-shim

ECMAScript 5 compatibility shims for legacy (and modern) JavaScript engines
MIT License
7.12k stars 895 forks source link

Cannot convert undefined or null to object PropTypes.shape() #461

Closed 1Jesper1 closed 5 years ago

1Jesper1 commented 5 years ago

https://github.com/es-shims/es5-shim/blob/8b0c47f4930507392d506ad956a740423dffab85/es5-shim.js#L1145

Proptype PropTypes.shape() gives error "Cannot convert undefined or null to object"

ljharb commented 5 years ago

I'm not sure what you mean. PropTypes.shape only works with an object, not with undefined or null, and Object.keys always throws with null or undefined. PropTypes.shape() is incorrect.

1Jesper1 commented 5 years ago

@ljharb React doesn't require it: See https://github.com/yannickcr/eslint-plugin-react/issues/1471

ljharb commented 5 years ago

@1Jesper1 if they're passing the argument value into Object.keys() then they do require it, since that always throws on null or undefined.

That linked issue is about the linter crashing on it, which is unrelated.

Can you provide the full stack trace, and the code for your shape creation?

1Jesper1 commented 5 years ago

Using:

static propTypes = {
        style: PropTypes.shape(),
    }

crashes on return originalKeys(object);

ljharb commented 5 years ago

and if you don’t include the shim, you’re saying it doesn’t crash? (note that you should also be using the es6-shim as well)

1Jesper1 commented 5 years ago

It doesn't crash when I don' include require("airbnb-js-shims"); https://github.com/airbnb/js-shims

ljharb commented 5 years ago

That's very bizarre. Can you provide the entire stack trace?

1Jesper1 commented 5 years ago
 TypeError: Cannot convert undefined or null to object
    at keys (<anonymous>)
    at Function.keys (http://localhost:9001/static/iframe.bundle.js:42468:24)
    at Shape.render (http://localhost:9001/static/iframe.bundle.js:18793:39)
    at finishClassComponent (http://localhost:9001/static/iframe.bundle.js:155929:31)
    at updateClassComponent (http://localhost:9001/static/iframe.bundle.js:155884:24)
    at beginWork (http://localhost:9001/static/iframe.bundle.js:156832:16)
    at performUnitOfWork (http://localhost:9001/static/iframe.bundle.js:160500:12)
    at workLoop (http://localhost:9001/static/iframe.bundle.js:160540:24)
    at renderRoot (http://localhost:9001/static/iframe.bundle.js:160623:7)
    at performWorkOnRoot (http://localhost:9001/static/iframe.bundle.js:161530:7)

I think the problem is in Storybook Shape.js class: Object.keys(propTypes).map(function

1Jesper1 commented 5 years ago

and if you don’t include the shim, you’re saying it doesn’t crash? (note that you should also be using the es6-shim as wel

Sorry, made the wrong assumption, still errors when not including the shim. The problem lies in Storybook. This thread can be closed.