`prop-types` fails with destructuring
See original GitHub issueThe prop-types
rule fails when presented with destructuring:
package.json
{
"dependencies": {
"eslint": "^4.6.1",
"eslint-plugin-react": "^7.3.0",
"proptypes": "^1.1.0",
"react": "^15.6.1"
}
}
.eslintrc
{
"env": {
"es6": true,
"node": true
},
"parserOptions": {
"sourceType": "module"
},
"plugins": ["eslint-plugin-react"],
"rules": {
"react/prop-types": 2
}
}
Examples.jsx
import React from 'react';
import PropTypes from 'proptypes';
// FAIL: Should error that `bar` is missing in propTypes
class DestructuredDeepMissing extends React.PureComponent {
render () {
const {
foo: {
bar,
},
} = this.props;
const it = bar;
}
}
DestructuredDeepMissing.propTypes = {
foo: PropTypes.shape({}).isRequired,
};
// Interestingly, non-deep destructuring works just fine
// PASS: Should error
class DestructuredMissing extends React.PureComponent {
render () {
const {
foo,
} = this.props;
const it = foo;
}
}
DestructuredMissing.propTypes = {};
Related: #296 Possibly related: #1346
Also possibly related is that no-unused-prop-types
(without using Flow) fails on destructuring as well, which is behavior I was able to replicate (though there seem to be a plethora of tickets related to this, especially in the Flow context).
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Object Destructuring eslint throws react/prop-types
propTypes = { id: PropTypes.oneOfType(PropTypes.number, PropTypes.string), borderColor: PropTypes.string, title: PropTypes.string, ...
Read more >rename doesn't work for destructuring props with propTypes
JSX: rename doesn't work for destructuring props with propTypes. 1. Relates to 1 Is duplicated by 1. Relates to 1 issue (0 unresolved)....
Read more >Specify Types of Props With PropTypes - Intermediate React
The PropTypes library allows you to declare the type of props you expect to get from your components and trigger a warning if...
Read more >The Complete Guide To Prop-Types In React
The result is not what we wanted and React didn't show us any warning or error. That's exactly what PropTypes are made for....
Read more >How to destructure a React component props object
In this article I'll show you how to destructure the React props object with a ... because if you miss a property, you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ljharb looked into this tonight locally with latest master, it actually looks like this has been fixed in a commit between last release and today. The code above is erroring correctly. I believe this should be fixed when you publish a new version. Can you confirm?
https://github.com/yannickcr/eslint-plugin-react/issues/1958#issuecomment-421091965
Thanks
We have this test case:
Judging from that, it seems some basic shapes support exists and the example should actually be supported.
Indeed when I try the following snippet I actually get an error. But the destructuring doesn’t work with the shapes…