[Typescript] Question: Generic type arguments in JSX elements working with withStyles
See original GitHub issue- This is a v1.x issue (v0.x is no longer maintained).
- I have searched the issues of this repository and believe that this is not a duplicate.
I wanted to leverage the generic type argument in JSX elements feature introduced in ts 2.9.1. However, when it comes to exporting with withStyles
, I don’t know how to expose that generic type parameter to the outside world.
class MyComponent<T> extends React.PureComponent<MyComponentProps<T>> {...}
export default withStyles(styles)(MyComponent);
However, after doing this, when I call MyComponent
like below, it said that MyComponent
expects 0 type arguments, but provided 1. It seems like {}
was passed to MyComponent
as the default type parameter if none is specified.
<MyComponent<string> prop1={...} /> // error: expected 0 type arguments, but provided 1.
So, my question is how I can achieve generic type arguments in JSX element with HOC?
Your Environment
Tech | Version |
---|---|
Material-UI | v1.2.2 |
React | 16.4.1 |
browser | |
etc |
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:9 (4 by maintainers)
Top Results From Across the Web
[Typescript] Question: Generic type arguments in JSX ... - GitHub
I wanted to leverage the generic type argument in JSX elements ... Generic type arguments in JSX elements working with withStyles #11921.
Read more >Generic type arguments in JSX elements with withStyles
It's jaw-dropping that referencing a property type from a type argument in the extends clause works, but it seems to!
Read more >Generic type arguments in JSX elements with withStyles-Reactjs
I'd make two modifications: (1) introduce an extra SFC to avoid a cast, and (2) grab the outer props type from the wrapped...
Read more >Passing Generics to JSX Elements in TypeScript - Marius Schulz
TypeScript 2.9 added the ability to specify type arguments for generic JSX elements.
Read more >TypeScript - Material-UI
The problem is that the type of the flexDirection property is inferred as string , which is too arbitrary. To fix this, you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Edit: I am now recommending the following solution based on franklixuefei’s solution. It doesn’t require a change to Material UI. See the additional remarks on Stack Overflow.
@jasanst Take a look at a complete example below.
Note that this is the real code from my project. No modification made yet.
Usage: