Inline Props Types vs Named Interfaces
You'll learn when to define component props inline versus extracting them into named interfaces.
Two Approaches to Props Types
When typing component props in TypeScript, you have two main options:
Inline object types define the shape directly in the component signature:
function UserCard({ name, age }: { name: string; age: number }) {
return <div>{name} is {age} years old</div>;
}
Named interfaces extract the type definition:
interface UserCardProps {
name: string;
age: number;
}
function UserCard({ name, age }: UserCardProps) {
return <div>{name} is {age} years old</div>;
}
When to Use Each
Use inline types when:
- The component is small and self-contained
- Props are simple (2-3 properties max)
- The type won't be reused elsewhere
- You're prototyping or writing quick examples
Use named interfaces when:
- The component is exported from a module
- Props are complex or numerous (4+ properties)
- Other components or functions need the same type
- You want better error messages (the interface name appears in errors)
- Documentation tools will generate API docs
Think of it like variable naming: you might write arr.filter(x => x > 5) inline, but for complex logic you'd extract a named function. Same principle applies here.
Practical Example
Inline is fine for a simple local component:
function Badge({ text }: { text: string }) {
return <span className="badge">{text}</span>;
}
But a public component deserves a named interface:
export interface ButtonProps {
label: string;
variant?: 'primary' | 'secondary';
onClick?: () => void;
}
export function Button({ label, variant = 'primary', onClick }: ButtonProps) {
// ...
}
Key Takeaway: Use inline types for simple, local components; extract named interfaces for exported components or complex prop shapes to improve maintainability and type reusability.