-
Notifications
You must be signed in to change notification settings - Fork 12.8k
infer
keyword in the prop types of React.forwardRef
is always deducted to unknown
#36502
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
We'd need a simpler repro to be able to investigate this quickly |
@RyanCavanaugh Thank you for the quick response! I created a concise reproduction available on the Playground: import React from 'react';
type TestProps<T> = {
children: T;
}
const CorrectComponent =
<C extends React.ComponentType<C extends React.ComponentType<infer P> ? P : never>>(
{ children }: TestProps<C>,
) => {
const Element: React.ElementType = children;
return <Element />;
}
;
const IncorrectComponent = React.forwardRef(
<C extends React.ComponentType<C extends React.ComponentType<infer P> ? P : never>>(
{ children }: TestProps<C>,
ref: React.Ref<Element>,
) => {
const Element: React.ElementType = children;
return <Element />;
}
);
function Example() {
return (
<>
<CorrectComponent>{CorrectComponent}</CorrectComponent>
<CorrectComponent>{IncorrectComponent}</CorrectComponent>
<IncorrectComponent>{CorrectComponent}</IncorrectComponent>
<IncorrectComponent>{IncorrectComponent}</IncorrectComponent>
</>
);
} In the latter case, |
This is possibly a duplicate of #9366. I apologize for not discovering that issue earlier. |
As far as I can tell this is still not working properly in your first example, even on latest TS versions @kripod |
I am pretty sure I have the same issue produced by this code.
|
@kripod is the reported issue fixed ? Thanks |
The issue seems to be fixed. @GuillaumeSpera Unfortunately, I'm not quite sure about the fix for my original code. As the problem has been fixed, I'm keeping this closed for now. Please open a new issue if you experience something wrong. |
TypeScript Version: 3.8.0-dev.20200128
Search Terms: infer, forwardRef, generics
Code
Expected behavior:
Box
should behave exactly likeBoxWithoutRef
from a type-checking perspective.Actual behavior:
React.ComponentType<C extends React.ComponentType<infer P> ? P : never>
insideBox
is always resolved asReact.ComponentType<unknown>
, causing false-positive type errors.Playground Link: http://www.typescriptlang.org/play/?jsx=2&ts=3.8.0-dev.20200128&ssl=51&ssc=27&pln=51&pc=30#code/JYW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J34qBJjp8FJVAyRMWYfjhbo1nWMANvxMRpCg4Dwk+S2kTxZ1+QlIzmrDHGTAAVHAO12EgaBFtWpZI29tUTCMwFwW2BqdhSACMIxUETPWk318MJQJNPqR2fUWTkt6qZZobSZQjOBYTza06zY4+LSABEKDi1wV3i9wvACMIraSDIcifDYECJOLDP8wATPjAnU0LLNkwbgtpMTvVjpwsvy-wghUCIYgq7IIBUOrGia9rADMVPMyTItUmb-tW8xfDXVEjC+LgAYGgKORZhSAAsfvGwHpv8wTId06L4dJpH0cgLHPB9FJaBRwKIChtdSe64nBsTAArGbRsW8LGeZ+bls52HEcVzHcel2R-dV0UNd1zLfUS3biuO8r0iu+7GtazrjMAGwt9nge1VTHFB-zIyI1QKMKRASlwLjq9Z8GFOp2kV9M8G7Oc7MiQ83jwdP2LkvSzbU9yzwe2StnYLzVsvbWDc4D60zobYCD9zaT0wNPQBs8nbiFAW7D2kAV6+1bvvTuwFe75xHsXeOicU54M-i5MWxDC6kKHuXQu1cky13rjApum8b5UKIQXSu9CE7DyYWPFhE8-5IIAQrB2aCXZgM9ivSBG8YF31XrvYAt4GZU3NN9WS5BvRQF9KwJ4JDB45D-CRR4uhgCuESF0dMnUpC7giJrJAFAJi+AUPCOxyU6B0FDP4f6KhXGCMrqGDgtdsyUA9FWfSvpGBqWrLWesjZeEDxLjkF8jx3HwgmnVB6v5hgAQzMYtJf48LjjovOcxEwoZQH4jDAW+o0m1VKBLRp8dxa1SyUga6SZqx9BgHQYAAAvdgBBvYInwKaUSXS4BZhzPze6YlHp-nmYFPGExtE5iAA
Related Issues: Possibly #9366
The text was updated successfully, but these errors were encountered: