RecipesChapter 10: TypeScript and React

Typing Polymorphic Components

Recipe 10.7 from The TypeScript Cookbook

function Cta<T extends CtaElements>({
  as: Component,
  children,
  ...props
}: CtaProps<T>) {
  return React.createElement(Component, props, children);
}
Open in TypeScript Playground →