The way React's
forwardRef
is implemented in TypeScript has some annoying limitations. The biggest is that it disables inference on generic components.
What Is A Generic Component?
A common use case for a generic component is a
Table
:
const Table = <T ,>(props : {
data : T [];
renderRow : (row : T ) => React .ReactNode ;
}) => {
return (
<table >
<tbody >
{props .data .map ((item , index ) => (
<props .renderRow key ={index } {...item } />
</tbody >
</table >
Here, when we pass in an array of something to data
, it will then infer that type in the argument passed to the renderRow
function.
<Table
// 1. Data is a string here...
data ={["a", "b"]}
// 2. So ends up inferring as a string in renderRow.
renderRow ={(row ) => {