Course contentsShow
← TypeScript
Lesson 1518 of 1,79635. TypeScript with Framework Cross-ReferencesFree lesson

Typing Component Props with Interfaces

Define prop types using interfaces for React components with proper TypeScript conventions.

Typing Component Props with Interfaces

You'll learn: How to define type-safe component props using interfaces to ensure your React components receive the correct data with full autocomplete and error checking.


What Are Component Props Interfaces?

In React components, props are the data passed from parent to child components. By defining these props with TypeScript interfaces, you create a contract that specifies exactly what data your component expects, what's required, and what's optional.

Think of a component props interface like a form that must be filled out correctly before the component can render. If someone tries to pass the wrong type of data or forgets a required field, TypeScript catches it immediately.

The Pattern

When creating a React component, you first define an interface describing the shape of its props, then use that interface to type the component's parameter:

interface ButtonProps {
  label: string;
  onClick: () => void;
  disabled?: boolean;
}

function Button({ label, onClick, disabled }: ButtonProps) {
  return (
    <button onClick={onClick} disabled={disabled}>
      {label}
    </button>
  );
}

Why Interfaces Over Type Aliases?

While you could use type aliases for props, interfaces are the community convention for React component props because:

  • Declaration merging allows library augmentation if needed
  • Error messages display interface names clearly
  • Consistency makes codebases more predictable

Combining Features

You can leverage optional properties, readonly modifiers, and function types within your props interfaces:

interface CardProps {
  readonly title: string;
  description?: string;
  onClose?: () => void;
  metadata: {
    author: string;
    date: string;
  };
}

This interface enforces that title must be provided and shouldn't be modified, while description and onClose are optional.


Key Takeaway: Define component props using interfaces with clear property types, optional modifiers, and function signatures—this gives you compile-time safety, excellent autocomplete, and self-documenting component APIs.