-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
react/no-unused-prop-types
false positive if any one component doesn't use a field
#2849
Comments
This seems like a correct warning to me. Reusing the type in both components means both props are required in both of them. In this case, you shouldn't be sharing the Props type at all, since clearly the two components' types do not overlap. |
Thank you for the reply (and adding the labels - apologies for not doing so). I think I can be persuaded that the warning is indicative of a code smell in most cases. I needed to keep type HasName = {
name: string;
}
type HasPlaceholder = {
placeholder: string;
}
type Props = HasName & HasPlaceholder; The reason I thought it was counter-intuitive is that the linter was telling me "this prop is unused", when I could see that it clearly was being used. Would you be happy with me raising a PR to add my contrived code as an example of incorrect code for this rule? |
Absolutely, that’d be helpful! I’d still love to understand your actual use case tho - not why you need the union type, but why the components need to use the overly broad union instead of the more granular type that matches their respective props. |
PR is up. To be clear, in my use case I don't think the components actually need to use the overly broad union; I've refactored to avoid this, which has resolved our linting error. One of the 'granular' types involved is a moderately complicated generic type from a third party library ( I am fairly confident that what we were doing was a bit of a code smell and the rule did help us spot the problem. Hopefully the addition to the docs will make it easier for the next person who encounters something similar. |
In the following example both
name
andplaceholder
are triggering theno-unused-prop-types
rule, despite both being used in separate components. I think the rule is triggering forname
because it is unused inComp1
andplaceholder
because it is unused inComp2
.I appreciate that this example is contrived, but I think is the most basic reproduction case of a more complicated manifestation of the issue. The behaviour is kind of logical but not exactly intuitive.
The text was updated successfully, but these errors were encountered: