I believe this is possible but I'm not too good with advanced typing in TS (yet) so:
I want to have a React component accept an array of any object shape in one prop, and then emit that same type in a different (event function) prop.
interface Props {
data: AnyGenericRow[];
onRow: (row: AnyGenericRow) => void;
}
How should I type AnyGenericRow to achieve what I want? I suppose I need to infer the type from data in some way and then apply that inferred type to the onRow signature.
Example usage:
const rows: Array<{ foo: 'bar' }> = [];
/* ... */
<Component data={rows} onRow={row => {
/* can `row` be detected as a type of { foo: 'bar' } ? */ }
} />
Perhaps this is super simple but I find it a bit tricky to know exactly what terms to search for to find this.
Bonus question: Can the inferred generic row extend an interface in the component? Perhaps all it takes is to & it...?