Writing a generic component
You need to show a list of strings. Next week, the same list of numbers. The shape never changes — a container, one child per item — only the item's type does. Instead of writing the component once per type, make it generic over the item type with a type parameter:
interface ListProps<T> {
items: T[];
render: (item: T) => ReactNode;
}
function List<T>({ items, render }: ListProps<T>) {
return (
<ul>
{items.map((item, index) => (
<li key={index}>{render(item)}</li>
))}
</ul>
);
}
<T> is the item type. items is a T[], and render receives one T. When
you use the component, TypeScript infers T from the props, so the same
component works for strings, numbers, or objects with no repeated code:
<List items={['a', 'b']} render={(item) => <span>{item}</span>} />
Inside render, item is inferred as string. The component stays typed and
the call site stays checked.
Your job is to write a generic list component in App.tsx.
Challenge
A list component only works for one kind of item, and it should work for any.
Export an App component from App.tsx that renders a generic List with
items={['a', 'b']} and a render prop that wraps each item in a <span>.
List is generic over its item type, takes items and a render function, and
renders each result as an <li> inside a <ul>.
The Test renders your component and expects the list to contain the items a
and b. The starter renders an empty list, so the Test fails.