Sub topic 3
Optional props and default values
This chapter assumes you know basic React function components, TypeScript interfaces and unions, and have a project already set up for TSX with strict on. It builds on typing props with an interface. The goal here is to mark props as optional, give them defaults, and understand what the compiler knows about them.
Marking a prop optional
A ? after a prop name makes it optional. Callers may leave it out, and inside the component its type becomes the declared type plus undefined.
interface BadgeProps {
label: string;
tone?: "info" | "warning";
}
export function Badge({ label, tone }: BadgeProps) {
// tone is "info" | "warning" | undefined here
return <span className={tone ? `badge-${tone}` : "badge"}>{label}</span>;
}With strict on, you cannot use tone as if it were always defined. You must check it or give it a default.
Giving a prop a default value
The simplest default is a destructuring default. The Card component from the spine uses this:
import type { ReactNode } from "react";
interface CardProps {
title: string;
variant?: "plain" | "outlined";
children: ReactNode;
}
export function Card({ title, variant = "plain", children }: CardProps) {
return (
<section className={`card card-${variant}`}>
<h2>{title}</h2>
{children}
</section>
);
}Inside the function, variant has the type "plain" | "outlined". The default removes undefined from the type, so the template string needs no check. Callers still see variant as optional.
What the default does and does not cover
A destructuring default applies only when the value is undefined. It does not apply to null, 0, "", or false.
interface PagerProps {
pageSize?: number;
}
export function Pager({ pageSize = 20 }: PagerProps) {
return <p>Showing {pageSize} rows</p>;
}
// <Pager /> shows 20
// <Pager pageSize={undefined} /> shows 20
// <Pager pageSize={0} /> shows 0, the default is not usedIf 0 is not a valid value for your component, validate it. The type system will not do it for you.
Optional versus a union with undefined
tone?: string and tone: string | undefined are different for callers. The first lets them omit the prop. The second forces them to write tone={undefined} explicitly. Use ? for props that really are optional.
| Declaration | Caller may omit it | Type inside (no default) | Column | Column |
|---|---|---|---|---|
| `tone?: string` | Yes | `string \ | undefined` | |
| `tone: string \ | undefined` | No | `string \ | undefined` |
| `tone: string` | No | `string` |
Defaults for function props
Optional callbacks need a check before you call them. You can also default them to a no-op.
interface ConfirmProps {
message: string;
onConfirm?: () => void;
}
export function Confirm({ message, onConfirm = () => {} }: ConfirmProps) {
return <button onClick={onConfirm}>{message}</button>;
}Without the default, you would write onConfirm?.() inside a handler.
Common errors
- Using an optional prop without narrowing.
tone.toUpperCase()fails with "possibly undefined". Add a default, or check first. - Defaulting with
||.pageSize || 20replaces0as well. Prefer a destructuring default or??. - Declaring a default in the interface. An interface describes types only. It cannot hold values, so put the default in the function signature.
- Mutable object defaults. A default such as
options = {}creates a new object on every render. This can trigger effects that depend on it. Define a constant outside the component and use that as the default.
Exercise
A component declares size?: number and destructures it as { size = 16 }. What are the type of size inside the component, and the value when a caller passes size={0}?
Show answer
Inside the component size is number, because the default removes undefined. When the caller passes 0, the value is 0. The default applies only to undefined, so 16 is not used.
Exercise
Why can tone: string | undefined be a worse choice than tone?: string for a prop that callers often skip?
Show answer
With tone: string | undefined the prop is required, so every caller must write tone={undefined} or pass a value. tone?: string lets callers omit the prop entirely.