Issue with generic syntax in .tsx
See original GitHub issueDescribe the bug
This is a common hack to use a comma to declare generics without default or extends in TSX file. It’s seems that some part of the build chain (in dev mode) drop the comma, making the file invalid.
Reproduction
https://github.com/ArnaudBarre/vite-generic-bug
Equivalent to yarn create @vitejs/app --template react-ts
+ update main.tsx
to:
const App = <T,>(props: {value: T}) => {
return <div>Hello {typeof props.value}</div>
}
ReactDOM.render(
<React.StrictMode>
<App value="string" />
</React.StrictMode>,
document.getElementById('root')
)
System Info
vite
version: 2.0.4 (esbuild 0.8.53)- Operating System: Mac
- Node version: 14.13.1
- Package manager: yarn 1.22.10
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Typescript TSX and generic parameters - Stack Overflow
@DanielRosenwasser I've run into a lot of trouble trying to use extends {} because in that case your function wouldn't accept something pretty...
Read more >TypeScript Generic Function Reported As JSX Error
To resolve the issue, I started with researching on how to properly define a generic function in TypeScript. Perhaps I made a mistake...
Read more >Passing Generics to JSX Elements in TypeScript - Marius Schulz
This component works as expected! Let's now statically type it in TypeScript and TSX. #Step #2: Implementing Select in TypeScript/TSX. We'll ...
Read more >JSX/TSX files with component tags using generics fail to parse ...
JSX/TSX files with component tags using generics fail to parse correctly. Yet ReSharper still fails to parse those correctly, despite TypeScript language level ......
Read more >Using Generic Arrow Functions in .tsx Files - Ashby
In standard Typescript, you can make an arrow function generic by declaring the type variable before the arguments:
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 FreeTop 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
Top GitHub Comments
The issue is coming from babel typescript or jsx parser (used in react refresh plugin). I opened an issue in the babel repo.
In the meantime, using
<T extends unknown>
worksThis code works with CRA, vite build, esbuild directly, and tsc. Just not with vite dev. I think there is an issue with the pipeline that doesn’t not take into account that the comma is important when loading tsx files for the reason that TSX ans TS generics are different (the code source has a comma, the error message no. )