Skip to content

QuizNot attempted

Extending native button props

How do you let a custom <Button isLoading> also accept every normal button attribute (disabled, aria-*, onClick…)?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • type Props = ComponentProps<'button'> & { isLoading?: boolean }
  • ComponentProps<'button'> (or ComponentPropsWithoutRef<'button'>) is exactly the set of props React accepts on <button>
  • intersect your own and spread ...rest onto the element.
  • Not listing by hand: incomplete and drifts from React's types.
  • Not any: loses all checking.
  • Not extends HTMLElement: that is the DOM node type (methods like focus()), not JSX props.