Hello! Welcome back to the second module of our course, "Components, Props, and Composition."
Introduction
In our last lesson, we established a solid foundation by exploring what functional components are and, more importantly, why they have become the undisputed standard in modern React development. We saw that they offer a simpler, more direct way to build UI compared to their class-based predecessors, thanks to the power of Hooks.
Today, we'll build directly on that knowledge to address the learning outcome: Pass data to components using props with TypeScript type definitions. We'll move from creating static components to dynamic ones by learning how to pass information from a parent component down to a child. This mechanism, called "props," is fundamental to building any React application.
What Are Props?
In React, props (short for "properties") are the primary way to pass data from a parent component to a child component. Think of them as the arguments you pass to a JavaScript function. The component is the function, the props are its parameters, and the JSX it returns is the function's return value.
This flow of data is unidirectional, often called a "one-way data flow." Data flows down the component tree from parents to children. This makes the application's logic predictable and easier to debug. A child component receives props but should never modify them directly; they are considered read-only.

Let's imagine a simple scenario. We have an App component that wants to display a personalized greeting using a reusable Greeting component.
In your src directory, you might have:
App.tsx (Parent Component)
import Greeting from './Greeting';
function App() {
return (
<div>
<h1>Welcome to My App</h1>
<Greeting name="Alice" />
</div>
);
}
export default App;
Greeting.tsx (Child Component)
// Initially, let's see what happens without types
const Greeting = ({ name }) => {
return <h2>Hello, {name}!</h2>;
};
export default Greeting;
If you try to run this code in a TypeScript environment, your editor and compiler will immediately flag an issue in Greeting.tsx:
Binding element 'name' implicitly has an 'any' type.
TypeScript is doing its job, telling us it doesn't know what name is or what other properties might be available on the props object. This is where explicitly defining prop types becomes crucial.
Typing Props with TypeScript
To fix this and gain the full benefits of TypeScript—like type safety, autocompletion, and self-documenting code—we need to define the "shape" of our props object. There are three common ways to do this.
Let's explore them with an excellent article from TypeScript expert Matt Pocock.
Strongly Typing React Props with TypeScript
The article 'Strongly Typing React Props with TypeScript' clearly outlines the three main approaches for declaring prop types. Understanding each one will help you choose the best method for your situation.
Please read the section titled 'The 3 Methods of Typing Props'. It covers 'Inline Object Literals', 'Type Aliases', and 'Interfaces', with clear code examples for each.
As the article shows, the three methods are:
-
Inline Object Literals: You define the type directly where the props are declared.
const Greeting = ({ name }: { name: string }) => { return <h2>Hello, {name}!</h2>; };This is quick for very simple components but isn't reusable and can become messy.
-
Type Aliases: You define a
typefor the props, which can be exported and reused.export type GreetingProps = { name: string; }; const Greeting = ({ name }: GreetingProps) => { return <h2>Hello, {name}!</h2>; }; -
Interfaces: You define an
interfacefor the props, which can also be exported and reused.export interface GreetingProps { name: string; } const Greeting = ({ name }: GreetingProps) => { return <h2>Hello, {name}!</h2>; };
type vs. interface: Which One to Choose?
You've likely encountered both type and interface in your TypeScript work. For defining component props, both are viable, but the community has developed some best practices based on their technical differences.
The "React TypeScript Cheatsheet" offers a fantastic, detailed comparison.
Typing Component Props - React TypeScript Cheatsheets
Let's consult the 'React TypeScript Cheatsheet' for a structured breakdown of this common question. It provides a clear rule of thumb and a detailed comparison.
Read the entire section 'Types or Interfaces?'. The comparison table is particularly useful for quickly grasping the differences.
Here's the comparison table from that resource, which provides a great visual summary:

The Recommendation for React Props:
For most general-purpose type definitions in TypeScript, the choice is largely stylistic. However, for React component props, interface is often the recommended choice.
The primary reason, especially relevant for large and complex applications, is performance. When you need to combine or extend prop types (a very common scenario), interface extends ... is handled more efficiently by the TypeScript compiler than the equivalent type alias using an intersection (&).
Let's return to the Total TypeScript article to see this argument explained.
Strongly Typing React Props with TypeScript
The performance argument is a key differentiator that will resonate with your engineering background. This article explains why interface has an edge.
Read the sections 'Interfaces over Intersections' and 'Final Thoughts'. This provides the official reasoning behind preferring interfaces for extending types.
So, the rule of thumb is: Use interface for defining component props. It's the most performant and scalable approach. Use type when you need features that interfaces don't support, like defining union types.
Practical Implementation and Advanced Patterns
Let's see these concepts applied in a practical coding demonstration. This video also introduces a powerful pattern for creating type-safe wrapper components that can accept standard HTML attributes.
TypeScript & React: Tips, Tricks, and Best Practices
This video from Jolly Coding provides a concise walkthrough of typing props with an interface and demonstrates the extremely useful pattern of extending native element props.
First, watch from 00:24 to 02:19 to see the basic setup of a prop interface. Then, skip to 02:50 and watch until 04:11 to learn how to create a wrapper component that inherits the props of a standard HTML element like <button>.
Let's solidify that second, more advanced pattern. Imagine you're building a custom Button component. You want it to have a special variant prop, but you also want users to be able to pass any standard button attribute, like onClick, disabled, or aria-label, without you having to define them all manually.
This is where extends React.ComponentPropsWithoutRef<'element'> comes in.
import { ComponentPropsWithoutRef } from 'react';
// 1. Define the interface for our custom button's props
export interface ButtonProps extends ComponentPropsWithoutRef<'button'> {
variant: 'primary' | 'secondary';
}
// 2. Create the component, destructuring our custom prop and gathering the 'rest'
const CustomButton = ({ variant, children, ...rest }: ButtonProps) => {
// Use the variant prop to apply a specific class
const className = `btn btn-${variant}`;
// 3. Spread the 'rest' of the props onto the underlying button element
return (
<button className={className} {...rest}>
{children}
</button>
);
};
// 4. Usage
const App = () => {
return (
<div>
<CustomButton variant="primary" onClick={() => alert('Primary clicked!')}>
Primary Button
</CustomButton>
<CustomButton variant="secondary" disabled>
Secondary Button
</CustomButton>
</div>
);
};
In this example:
ButtonPropsgets all the properties of a standard HTML<button>element.- We add our own
variantprop to the interface. - In the component, we use the rest syntax (
...rest) to collect all props exceptvariantandchildren. - We then spread these
restprops onto the<button>element. This automatically passes downonClick,disabled, and any other valid button attribute with full TypeScript safety.
Test your understanding!
You are tasked with creating a UserProfileCard component. This component needs to display a user's information.
Requirements:
-
The component must accept the following props:
name: astringage: anumberisVerified: abooleanthat determines if a "Verified" badge is shown.
-
Use an
interfaceto define the prop types. -
The component should render the user's name, age, and conditionally display the "Verified" badge.
Write the UserProfileCard component based on these requirements.
Show answer
Here is one possible solution:
import React from 'react';
// 1. Define the props using an interface
export interface UserProfileCardProps {
name: string;
age: number;
isVerified: boolean;
}
// 2. Create the component and type its props
const UserProfileCard = ({ name, age, isVerified }: UserProfileCardProps) => {
return (
<div style={{ border: '1px solid #ccc', padding: '16px', borderRadius: '8px' }}>
<h3>{name}</h3>
<p>Age: {age}</p>
{isVerified && <span style={{ color: 'green' }}>✓ Verified</span>}
</div>
);
};
// Example of how you would use it in a parent component:
const UserList = () => {
return (
<div>
<UserProfileCard name="Satoshi" age={25} isVerified={true} />
<UserProfileCard name="Yui" age={22} isVerified={false} />
</div>
)
}
export default UserProfileCard;
Conclusion
You've now learned the essential mechanism for making components dynamic and reusable. By passing data through props, you can configure and control child components from their parents, creating a robust and predictable application architecture.
Key Takeaways:
- Props are read-only objects used to pass data down the component tree from parent to child.
- Using TypeScript to define the shape of your props object is critical for type safety, maintainability, and developer experience.
- You can define prop types using inline objects,
typealiases, orinterfaces. - For React component props,
interfaceis generally the preferred method due to its better performance when extending other types and its semantic clarity. - You can create powerful, type-safe wrapper components by using
interface extends ComponentPropsWithoutRef<'element'>.
Next Steps
We've seen how to define the types for the props our components expect. But what if a parent component doesn't provide an optional prop? In our next lesson, we will learn how to define default props using standard JavaScript syntax, ensuring our components always have sensible fallbacks and behave predictably.