Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-unused-prop-types returns false positive when destructuring props cast to a different type #2517

Closed
kdmadej opened this issue Dec 10, 2019 · 4 comments · Fixed by #2536
Closed

Comments

@kdmadej
Copy link
Contributor

kdmadej commented Dec 10, 2019

Sample Code

const propTypes = {
  foo: PropTypes.number.isRequired,
};

type Props = PropTypes.InferProps<typeof propTypes>;

const Foo: React.FC<Props> = (props) => {
  const {
    foo,
  } = props as any; // works if `as any` assertion is removed

  return (
    <>{foo}</>
  );
};

Issue

The linter throws an error: 'foo' PropType is defined but prop is never used even tho the prop is used.

Expected

The linter should not care about props being asserted to be of a different type. If an assertion is made it's most likely for a good reason and the actual prop is still referenced inside the component (no matter its type).

@ljharb
Copy link
Member

ljharb commented Dec 10, 2019

Although any use of any defeats the entire purpose of using TypeScript, I assume if you do as string or similar you see the same problem - in which case it seems like a straightforward bug.

@kdmadej
Copy link
Contributor Author

kdmadej commented Dec 10, 2019

Yup I should've made it more clear in the example that any kind of assertion causes a failure. I encountered the behaviour when casting to a generic that makes use of defaultProps to consider those props that have a default value to be non-nullable, any was just the first thing that came to my mind when writing a sample 😉

I'd be more than happy to try and prepare a PR but I'd need some guidance regarding where to start – I don't have any experience with eslint internals, neither its plugin ecosystem 😅

@ljharb
Copy link
Member

ljharb commented Dec 10, 2019

@kdmadej
Copy link
Contributor Author

kdmadej commented Dec 11, 2019

Thanks! I'll have a go at it later this week 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants