TypeScript · Middle · Question 22 of 75
How do you type a generic list component?
function List<T>({ items, render }: {
items: T[];
render: (item: T) => React.ReactNode;
}) { /* ... */ }- ✓Correct answer: Declare a type parameter on the component function - TS infers
Tfromitems - 2Generics aren't supported in React components because of JSX syntax
- 3Use
React.FC<T>, whereTis the list item type
Explanation
<List items={users} render={u => u.name} /> - T is inferred as User, and u in render is typed. In .tsx files, arrow functions use <T,> so the parser doesn't mistake it for JSX. React.FC doesn't pass generics through.