Skip to content

QuizNot attempted

Generic List infers T from items

What is the type of item inside onClick?
type ListProps<T> = { items: T[]; onClick: (item: T) => void };
function List<T>({ items, onClick }: ListProps<T>) { /* … */ }
<List items={[{ id: 1, first: 'Bruce' }]} onClick={(item) => console.log(item)} />
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • { id: number; first: string }
  • JSX props are inferred like function arguments: items gives `T = { id: number
  • first: string } and onClick's parameter is then contextually typed from T`, so a single generic component serves strings, numbers and objects.
  • Not unknown: that is only what you get when nothing constrains T.
  • Not any: generics keep the type.
  • Not the literal type: object literal properties are widened (1 to number) unless you use as const.