Sub topic 2
Typing props with interfaces and type aliases
This chapter assumes you know basic React (function components, props, state, JSX) and TypeScript fundamentals (interfaces, type aliases, unions, generics). It also assumes your project already compiles TSX in strict mode, as set up in the previous chapter.
Why type props
Props are the public contract of a component. A prop type tells the compiler which props a component accepts, which are required, and what each one holds. With strict mode on, a missing prop, a misspelled prop, or a wrong value type becomes a compile error instead of a runtime bug.
The basic pattern
Declare the props shape, then annotate the destructured parameter with it.
interface GreetingProps {
name: string;
age: number;
}
export function Greeting({ name, age }: GreetingProps) {
return (
<p>
{name} is {age} years old.
</p>
);
}TypeScript checks every place the component is used:
<Greeting name="Ada" age={36} /> // OK
<Greeting name="Ada" /> // Error: property 'age' is missing
<Greeting name="Ada" age="36" /> // Error: string is not assignable to number
<Greeting name="Ada" age={36} nick="A" /> // Error: 'nick' does not existThe compiler checks the destructured parameter against the interface, and it checks each JSX attribute against the same interface. You write the contract once.
Interface or type alias
Both describe props. For plain object shapes they behave almost the same.
interface BadgeProps {
label: string;
count: number;
}
type BadgeProps2 = {
label: string;
count: number;
};The differences matter in a few cases.
| Need | interface | type alias | Column |
|---|---|---|---|
| Plain object shape | Yes | Yes | |
| Union of shapes (`A \ | B`) | No | Yes |
| Extend another shape | `extends` | Intersection with `&` | |
| Merge declarations with the same name | Yes | No (duplicate name is an error) | |
| Map or compute over keys | No | Yes |
A practical rule: use interface for simple object props, and switch to type when you need a union or a computed type. Pick one style per codebase and stay consistent. Later chapters use unions for props, which require type.
Describing richer prop values
Props are not limited to primitives. You can type arrays, nested objects, string literal unions, and callbacks.
interface Product {
id: number;
title: string;
}
interface ProductListProps {
products: Product[];
size: "small" | "large";
onSelect: (id: number) => void;
}
export function ProductList({ products, size, onSelect }: ProductListProps) {
return (
<ul className={`list list-${size}`}>
{products.map((p) => (
<li key={p.id}>
<button onClick={() => onSelect(p.id)}>{p.title}</button>
</li>
))}
</ul>
);
}The literal union "small" | "large" rejects size="medium" at compile time. The callback type (id: number) => void makes the parent's handler check its parameter. Prefer a literal union over a bare string whenever only a few values are valid.
Common errors
- Annotating with
anyor leaving the parameter untyped. Understrict, an untyped destructured parameter is an error (implicitany). Do not silence it withany, because that removes every check described above. - Using
objector{}as a props type. These accept almost anything, so typos pass silently. Declare the real shape. - Putting the type in the wrong place. Write
({ name }: Props), not({ name: Props }). The second form renamesnameto a variable calledProps. - Typing props as optional to avoid errors. If a prop is always needed, keep it required. Optional props are covered in the next chapter.
Exercise
A component is declared as function Tag({ text }: { text: string }). A teammate renders <Tag text={42} />. What happens in strict mode, and what single change to the props type would make this call valid?
Show answer
The compiler reports that number is not assignable to string, because text is typed as string. Changing the type to text: string | number makes the call valid. Check that the body handles both types, for example by rendering the value directly, which works for both.
Exercise
You need props that are either { kind: "a"; x: number } or { kind: "b"; y: string }. Should you use interface or type, and why?
Show answer
Use type. A union of object shapes can only be written with a type alias (type Props = A | B). An interface describes a single object shape and cannot be a union.