-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
react/prop-types & TypeScript: false positive with HTMLAttributes prop type #3325
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
Comments
That is indeed bizarre, I'd expect them all to work, or not work, the same. Type resolution typically comes from the TS eslint resolver, so it's possible some of the issue is on that side (cc @bradzacher) but it'll need investigation here regardless. |
I'll start by saying that this rule seems like it is pretty redundant in TS code because TS will catch the case where you're using a property that's not declared in the props object type. I.e. the rule will double-report (and likely do a worse job than TS as it's not type-aware).
I don't think it does - it looks like the logic is hard coded to look for top-level type/interface declarations instead of using scope analysis: eslint-plugin-react/lib/util/propTypes.js Line 672 in 5919660
eslint-plugin-react/lib/util/propTypes.js Lines 681 to 684 in 5919660
There's a lot of code and utilities that your rules are built on top of - so it's hard to quickly decipher what the rule is doing specifically, but some quick poking around... I can see that here you use the right-most name (i.e. eslint-plugin-react/lib/util/propTypes.js Lines 638 to 640 in 5919660
to index this variable: eslint-plugin-react/lib/util/propTypes.js Lines 104 to 114 in 5919660
Which will return undefined , meaning the next line will return undefined :eslint-plugin-react/lib/util/propTypes.js Lines 641 to 642 in 5919660
and the traversal will completely exit: eslint-plugin-react/lib/util/propTypes.js Lines 591 to 592 in 5919660
So I think that what's happening is that the rule is just not registering any properties, nor is it marking the props as something the rule should ignore? |
Interesting, thanks, that's a really helpful analysis. I also agree that if TS will catch things that aren't declared, then it might be better for the prop-types rule to merely ensure that there is a TS type (presumably not |
Using
React.HTMLAttributes<HTMLButtonElement>
as the prop type in a component definition causes false positives ofreact/prop-types
.Strangely enough, when adding indirection by extending that same type in an interface, the problem goes away. The same technique applied to a type alias for the same type does not work, however.
Even stranger, the last case in the minimal working example below shows a case where prop type
T
gives the false positive whileT & T
does not.Version: [email protected]
Minimal working example:
The text was updated successfully, but these errors were encountered: