eslint-plugin-react: react/prop-types breaks when propTypes use spread operator

In a component similar to this one:

static propTypes = {
  foo: PropTypes.string,
}

componentDidMount() {
  const { foo, bar } = this.props;
  console.log(foo, bar);
}

react/prop-types complains that 'bar' is missing in props validation, which is correct.

However, if I change the propTypes to

static propTypes = {
  foo: PropTypes.string,
  ...{ other: PropTypes.bool, stuff: PropTypes.func },
}

eslint will pass, which seems to be a bug.

I did not find any similar issue here on github, but maybe I just missed it.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 17 (8 by maintainers)

Most upvoted comments

What you suggest is going against DRY quite a bit though and this is an extremely common React pattern so I don’t think people are going to stop doing it.

I’d suggest avoiding spreading props entirely; one big benefit is regaining the static analysis that doing so prevents.