-
-
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
[Bug]: react/prefer-read-only-props
doesn't work with implicit React
reference (react/jsx-runtime
)
#3650
Comments
Oof, that seems really unfortunate from TS - referencing React without importing it is horrific. The new jsx transform works because it doesn’t transform to something that references React. I’m not sure how we can handle this cleanly. |
@ljharb Can you point me to the source logic that recognizes |
It's in lib/util/propTypes.js, and yes, it's type-aware whenever the parser provides that information. The key is, we shouldn't be treating something as a React type unless we're certain it came from React. |
Even if the identifier is |
Btw, the rule already identifies a React component even without any import matching: interface ChipProps {
chipColor: string;
label: string;
}
const Chip = ({ chipColor, label }: ChipProps) => {
return <div style={{ backgroundColor: chipColor }}>{label}</div>;
}; Can't we use the same heuristic to assume that when we see |
Yes, we can never assume any identifier is anything specific; someone could make their own thing called that. a React component uses jsx syntax, and If it's TS that provides React.FC as an implicit global, then certainly we could look for when |
Indeed ![]() ![]() Considering this fact, I think it's harder to imagine someone using Side noteThis issue is also relevant for |
I'd still like to be precise about ensuring that |
Is there an existing issue for this?
Description Overview
Adding
react/prefer-read-only-props
rule does not trigger when usingReact.FC
orReact.FunctionComponent
without importingReact
.In modern React, we don't need to import
React
thanks to the new JSX Transform:And TypeScript supports this out of the box:
So even for React types we don't have to import them.
React.FC
just works in.tsx
files.But
react/prefer-read-only-props
fails to identify the component as a React component if we useReact.FC
orReact.FunctionComponent
without importingReact
.Expected Behavior
This code does NOT error
This code does
.eslintrc.js
tsconfig.json
Tried in VS Code and in the command line:
eslint-plugin-react version
7.33.2
eslint version
8.38.0
node version
v16.14.0
The text was updated successfully, but these errors were encountered: