React Props
React props are the object passed to a function component. In TypeScript you type that object, and JSX checks attributes against it. Extra attributes on a component are errors; missing required ones are errors.
Detailed Explanation
A function component is a function from props to JSX. The type of the first parameter is the props type:
type ButtonProps = { label: string; onClick: () => void };
function Button({ label, onClick }: ButtonProps) { ... }
<Button label="Save" onClick={save} /> is checked like a function call. <Button /> is missing label. <Button label="Save" onClick={save} extra /> is an excess property error. That is the same excess-property rule as object literals, applied to JSX attributes.
children is a prop. Type it as React.ReactNode when you accept anything renderable, or as a narrower type (string, a specific element) when you do not. PropsWithChildren<P> is P & { children?: ReactNode }. Do not use React.FC just to get children; it has historically changed and hides the props name. Write the parameter type.
Optional props are ?. Default values in the destructure ({ label = "OK" }) make the parameter optional at the call site and defined inside. Discriminated unions work on props: { kind: "link"; href: string } | { kind: "button"; onClick: () => void } makes href required only for links.
HTML attributes: if you wrap a native element, extend the right map. button is React.ButtonHTMLAttributes<HTMLButtonElement>. Spreading {...rest} then type-checks className, disabled, type. Extending HTMLAttributes is wider than you want for a button (type vs href). Pick the tag's attribute type.
ComponentProps<typeof Button> extracts the props type from a component. ComponentPropsWithoutRef<"button"> is the native attribute type without ref. Use these to wrap library components without copying their props by hand.
Props are erased. A missing runtime check on label still crashes if someone uses the component from JS. For app code, the type is enough. For a published component, validate or document the TS requirement.
See JSX types for the jsx compiler option and the React namespace.
Code Examples
1. A props type and JSX checking.
// 1. A props type and JSX checking.
type ButtonProps = {
label: string;
onClick: () => void;
disabled?: boolean;
};
function Button({ label, onClick, disabled = false }: ButtonProps) {
return (
<button type="button" onClick={onClick} disabled={disabled}>
{label}
</button>
);
}
const el = <Button label="Save" onClick={() => {}} />;2. Discriminated props.
// 2. Discriminated props.
type Action =
| { kind: "link"; href: string }
| { kind: "button"; onClick: () => void };
function ActionControl(props: Action) {
if (props.kind === "link") return <a href={props.href}>Go</a>;
return <button onClick={props.onClick}>Go</button>;
}3. Wrapping a native button.
// 3. Wrapping a native button.
type IconButtonProps = {
label: string;
} & React.ButtonHTMLAttributes<HTMLButtonElement>;
function IconButton({ label, ...rest }: IconButtonProps) {
return (
<button type="button" aria-label={label} {...rest}>
{label}
</button>
);
}Common Gotchas
Extra JSX attributes are type errors on your component, not on `div`
native elements have a huge attribute surface; your props type does not unless you extend it.
`React.FC` is optional and historically messy
type the props parameter instead.
Defaults in destructuring change optionality
`{ n = 0 }: { n?: number }` is the usual form.
Spreading untyped `rest` onto a DOM node can pass invalid attributes
type `rest` as the tag's attribute type.