Skip to content

Function that returns a component causes false positive unused react/prop-types warning in typescript #2784

Closed
@DamianPereira

Description

@DamianPereira

The folllowing code causes a false positive for the react/proptypes, with 'className' missing in prop-types declaration. Since this is typescript, and ComponentProps includes className, I'd expect this to not happen.

import * as React from "react";

type ComponentProps = {
  className?: string;
};

class Test {
  getAComponent(): React.FC<ComponentProps> {
    return function ComponentName(props: ComponentProps): React.ReactElement {
      return <div className={props.className}>Hello</div>;
    };
  }
} 

const test = new Test();
const Comp = test.getAComponent();

export default function App(): React.ReactElement {
  return (
    <div className="App">
      <Comp className="text-success" />
    </div>
  );
}

If the component is instead defined outside of the method that returns it, then the warning dissapears, like this (the rest of the example remains the same):

const getAComponentNotInClass = function ComponentName(props: ComponentProps): React.ReactElement {
  return <div className={props.className}>Hello</div>;
};

class Test {
  getAComponent(): React.FC<ComponentProps> {
    return getAComponentNotInClass
  }
} 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions