Course contentsShow
← React
Lesson 2339 of 2,39651. TypeScript with React BasicsFree lesson

Exporting Component Types for Composition

Export prop types for component composition and maintaining type safety across component boundaries.

Exporting Component Types for Composition

You'll learn how to export your component's prop types so other components can safely compose with them while maintaining TypeScript safety.

Why Export Types?

When you build reusable components, other developers (including future you) need to compose them—wrapping them, extending their props, or building higher-order patterns around them. Without exported types, they'd have to guess at your prop structure or duplicate type definitions, breaking the single source of truth.

Think of it like publishing an API specification. Your component is the implementation, but the type is the contract. Export both so consumers can build on solid ground.

The Pattern

// Button.tsx
export interface ButtonProps {
  variant: 'primary' | 'secondary';
  onClick: () => void;
  children: React.ReactNode;
}

export function Button({ variant, onClick, children }: ButtonProps) {
  return (
    <button className={variant} onClick={onClick}>
      {children}
    </button>
  );
}

Now other components can import and extend:

// IconButton.tsx
import { ButtonProps } from './Button';

interface IconButtonProps extends ButtonProps {
  icon: string;
}

export function IconButton({ icon, ...buttonProps }: IconButtonProps) {
  return (
    <Button {...buttonProps}>
      <span>{icon}</span>
      {buttonProps.children}
    </Button>
  );
}

The IconButton inherits type safety from ButtonProps—change variant in the original, and TypeScript catches breaks everywhere.

When to Export

Export prop types whenever your component is meant to be reused, wrapped, or extended. Keep internal-only components' types private.

Key Takeaway: Export component prop interfaces alongside the component itself to enable type-safe composition and maintain a single source of truth across your component boundaries.