eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

Bug: `no-unused-prop-types` doesn't detect unused props with Typescript & React 17 jsx transform

Open TristanHessellHyper opened this issue 2 years ago • 4 comments

The rule react/no-unused-prop-types doesn't seem to pick up that the component has unused props.

Environment:

Dependencies:

"typescript": "4.6.3",
"eslint-plugin-react": "7.30.1",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",

eslintrc.js

module.exports = {
  rules: [
    'react/no-unused-prop-types': 'error',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 11,
    project: './tsconfig.json',
  },

tsconfig.json

{
  "compilerOptions": {
   /* other config doesn't seem to matter*/
    "jsx": "react-jsx",
  },
}

Example Code

  type Props = {
    username: string;
  }

  const App: React.VFC<Props> = (props) => {
    return <div></div>;
  }

Expected Outcome

Lint error with message 'username' PropType is defined but prop is never used.

Actual Outcome

No error message.


If I change the code to have import React from 'react' at the top of the file, the rule works as expected.

TristanHessellHyper avatar Jul 13 '22 14:07 TristanHessellHyper

If you have React.VFC in the file, how can you not be importing React?

ljharb avatar Jul 15 '22 19:07 ljharb

@ljharb the code uses the react 17 jsx transform, which doesn't require React to be imported: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#removing-unused-react-imports.

In TypeScript, React isn't required to be imported (when used as a type namespace) if the compiler option is set.

TristanHessellHyper avatar Jul 16 '22 00:07 TristanHessellHyper

I'm aware of the jsx transform; i was not aware TS allowed sloppy type imports with any options provided.

If you explicitly import type the VFC type, what happens?

ljharb avatar Jul 16 '22 00:07 ljharb

When using import type {VFC} from 'react', the unused props are reported 👍

TristanHessellHyper avatar Jul 20 '22 11:07 TristanHessellHyper

Hi, I would like to work on this issue @ljharb

himanshu007-creator avatar Oct 14 '22 20:10 himanshu007-creator

Go for it!

ljharb avatar Oct 14 '22 20:10 ljharb