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;
}) { /* ... */ }

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.

Further reading

Problem with a question

TypeScript · Middle · question 22

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz