-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic JSX props don't work properly when props contain generic functions #20891
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
For posterity, this changed is due to your config containing As explanation for why what you've got changes: interface Props<T> {
a: T
b: T[]
} is fine. You'll note there are no function types in your props, so we don't do any strict variance checking on anything. Once you add c: (t: T) => number // This addition breaks it we start doing strict function type checking for the added property. |
@weswigham Thank you for the reply. From what I understand, variance doesn't come into play in my example, so this is not "working as intended", right? I just want to make sure I understand correctly whether you're explaining why the example doesn't and shouldn't work or why the bug? appears in 2.6. |
Oh, it's not working as intended, for sure. The issue is actually that since we don't do any type inference when instantiating JSX attributes, we end up comparing the generic with its constraint (as when we instantiate the target we fill the generics with their constraints), and strict variance checking causes us to invert the directionality of that check at a callback input position (because variance is inverted there, structurally), causing us to check if I'm just saying that you can disable |
@weswigham I understand where <ClassComponent
value={value as T}
// If I forget `: T` onChange it will became `any` which can cause runtime errors in some cases
onChange={(newValue: T) => {
value = newValue;
}}
/>; So in the end I find myself with looser typing |
@oriSomething #21383 should fix the typechecker behavior there (assuming the generics on |
In 2.5.3, I was using JSX components with generic props containing functions successfully by wrapping stateful ones with stateless components, as also described in #6395 and in this comment
However, in 2.6.x and typescript@next, it breaks:
TypeScript Version: 2.7.0-dev.20171224, 2.6.2
Code
Expected behavior:
I expect it to work. It worked on 2.5.3
Actual behavior:
It breaks as soon as I add generic functions to the props
Config used:
The example works as expected on 2.5.3.
The text was updated successfully, but these errors were encountered: