question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with generic syntax in .tsx

See original GitHub issue

Describe 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.

image

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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
ArnaudBarrecommented, Mar 7, 2021

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> works

0reactions
ArnaudBarrecommented, Mar 2, 2021

This 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. )

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found