Skip to content

QuizNot attempted

Generic components in .tsx

Why does const List = <T>(props: Props<T>) => … break in a .tsx file, and what's the fix?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • The parser reads <T> as a JSX tag; write <T,> or <T extends unknown>
  • In .tsx, <T> at the start of an arrow function parses as an opening JSX tag
  • <T,> or <T extends unknown> disambiguates.
  • Not 'arrows can't be generic': they can in .ts files and with the fix.
  • Not 'components can't take generic props': generic components are common (List<T>).
  • Not React.FC<T>: FC can't express a generic component.
  • Function declarations don't have the ambiguity.