-
-
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
False positives of react/prop-types in forwardRefs with 7.27.0 #3140
Comments
cc @vedadeepta |
This comment has been minimized.
This comment has been minimized.
I have noticed a potential cause/fix. The validation fails in the following case:
but it's all good when we change the name of the parameter to something else than
Do we have a false positive that matches only the component props? |
Interesting. On the other hand, the validation still fails when you destructure right away in the function parameters:
|
I've put together a reasonably minimal example that triggers this:
Output:
Remove the omission and the props are validated correctly:
Issue seems to be that |
@gunters63 i'm not able to replicate. I pulled the latest and put your code almost exactly in the test case and it passed, maybe some intermediate commit fixed it Here is the sample test case
cc @ljharb @nzdjb yes my PR didn't handle special TS types like Omit, Partial, Exclude etc. That will require a separate PR and I'm not sure about the effort. Not planning to do it in the next few weeks. Apologies. |
Ideally we wouldnt have to explicitly support such things; the TS parser should have the type information we need already in the AST, thereby handling every possible type. |
This starts to get mysterious :) If I inline the type definitions like this:
the error goes away. But my code originally does this:
Contents of $/components/BaseControl:
If I import the base types like the code initially does the error comes back. |
@gunters63 ahh i see, if i understand correctly, currently we cannot parse types coming from external files. @ljharb any thoughts on how to handle types coming from other files? |
I have many other components using those external types which do not use forwardRef. Those work fine. |
@vedadeepta the TS parser should provide all those types; we shouldn't have to figure them out ourselves. |
Can also confirm exporting the type in the same file breaks only for forward refs
The following is with the export: In saying this, I have no idea why exporting the type would break eslint from getting the proptype. 😕 seems similar to the discussion happening at #3015 |
recent release of eslint-plugin-react introduced a bug related to type checking props inside functions: - jsx-eslint/eslint-plugin-react#3140 - jsx-eslint/eslint-plugin-react#2760
@ljharb we're not getting the imported types in the AST. I checked the AST by creating a sample repo and importing types from other files. Also its not just that the imported types don't work with I don't think this case can be handled without using the TS compiler API which can get the types from other files. |
@vedadeepta: As long as this is not implemented, wouldn't it be better to remove the special handling of forwardRef introduced in commit 4bc3499? |
Maybe, I'm not sure. This does not work for other React generic like
So we will need to remove all ts handling since it cannot get types imported from other files |
In that case yes, we should remove it. |
Or at least, it should only warn when the entire type is defined in the same file? |
An intermediate solution would be to wrap the FC export like so: export interface IInputFieldProps extends InputHTMLAttributes<HTMLInputElement> {
...
}
const InputField = (props: IInputFieldProps, forwardedRef: Ref<HTMLInputElement>,) => {
return (
<>
<input {...props} ref={forwardedRef}></input>
</>
)
});
export default forwardRef<HTMLInputElement, IInputFieldProps>(InputField); |
hi, import React, { ForwardedRef, forwardRef } from "react";
export interface HelloWorldProps {
subject: string;
className?: string;
}
const HelloWorld = forwardRef(
function HelloWorldBase (
props: HelloWorldProps,
ref: ForwardRef<HtmlDivElement>,
) {
const { subject, className } = props;
return (
<div
className={className}
ref={ref}
>
Hello {subject}
</div>
);
},
);
export default HelloWorld; |
Nice find! This workaround is low-effort and works well, I finally can unpin the package version again :) |
@vedadeepta any chance you'd be able to work on a PR for this? |
@ljharb I can take it if you and @vedadeepta don't mind :) |
That'd be great! |
thanks @mobily |
prop type checking seems to broken inside forwardRef with version 7.27.0, 7.26.1 still worked fine.
The error only happens for type intersection hierarchies (I didn't test interface extensions).
When you have types like this:
The plugin will now falsely flag all properties in nested type intersections as missing in props validation, in this case name, className and children (see screenshot wiggly lines).
The problem does not happen when the component is not wrapped in forwardRef.
It also didn't happen with version 7.26.1
Its likely that this commit: 4bc3499 triggered the problem
The text was updated successfully, but these errors were encountered: