Sub topic 4
Typing children with ReactNode
This chapter assumes you know React function components, JSX, and TypeScript interfaces and unions. It also assumes your project already uses the strict and jsx: "react-jsx" settings from the setup chapter.
What children is
children is the content you place between a component's opening and closing tags. React passes it to the component as a normal prop named children. TypeScript does not add this prop for you, so you must declare it in your props type.
import type { ReactNode } from "react";
interface CardProps {
title: string;
children: ReactNode;
}
export function Card({ title, children }: CardProps) {
return (
<section>
<h2>{title}</h2>
{children}
</section>
);
}Why ReactNode
ReactNode is the widest type for anything React can render. It includes:
- JSX elements
- strings and numbers
null,undefined, and booleans (React renders nothing for these)- arrays of any of the above
Because of this, all of these calls type-check:
<Card title="A">Plain text</Card>
<Card title="B">{42}</Card>
<Card title="C">{isReady && <p>Ready</p>}</Card>
<Card title="D">
<p>One</p>
<p>Two</p>
</Card>The isReady && <p> case matters. When isReady is false, the expression is false, and ReactNode accepts it. A narrower type would reject this common pattern.
ReactNode, ReactElement, and JSX.Element
These three types are easy to confuse.
| Type | Accepts | Use it for |
|---|---|---|
| `ReactNode` | Anything renderable, including text, `null`, and arrays | `children` and other slots that can hold any content |
| `ReactElement` | Only a JSX element object | Props where text or `null` would be wrong |
| `JSX.Element` | A JSX element, in practice the same as `ReactElement` | Return types you write by hand |
A common mistake is typing children as JSX.Element. That rejects plain strings and the false from conditional rendering, and it rejects multiple children as well, because several children arrive as an array.
import type { ReactElement } from "react";
interface StrictProps {
children: ReactElement;
}
function Strict({ children }: StrictProps) {
return <div>{children}</div>;
}
// OK: one element
const a = <Strict><p>Hi</p></Strict>;
// Error: string is not assignable to ReactElement
const b = <Strict>Hi</Strict>;Use ReactElement only when you really need exactly one element, for example when you plan to call cloneElement on it.
Optional children
With children: ReactNode, callers must supply content. Passing nothing is an error under strict. If the component can render without content, make the prop optional.
import type { ReactNode } from "react";
interface PanelProps {
heading: string;
children?: ReactNode;
}
export function Panel({ heading, children }: PanelProps) {
return (
<div>
<h3>{heading}</h3>
{children ?? <p>Nothing to show.</p>}
</div>
);
}Choose between required and optional by what the component does. A Card with no content is probably a mistake, so require it. A Panel with an empty state is fine without it.
Using PropsWithChildren
React also exports PropsWithChildren<P>. It adds children?: ReactNode to the props type P you give it.
import type { PropsWithChildren } from "react";
type BoxProps = PropsWithChildren<{ padded: boolean }>;
export function Box({ padded, children }: BoxProps) {
return <div className={padded ? "box padded" : "box"}>{children}</div>;
}It saves a line, but the result is always optional. Writing children: ReactNode yourself is clearer and lets you make it required.
Common errors
Property 'children' does not exist on type: you destructuredchildrenbut never declared it in the props type.Type 'string' is not assignable to type 'ReactElement': you used an element-only type where text should be allowed. Switch toReactNode.- Objects rendered as children: a plain object such as
{ a: 1 }is not aReactNode. TypeScript flags it, and React would throw at runtime.
Exercise
A Modal component should always receive content, but callers sometimes pass a string and sometimes several elements. Which type should children have, and what happens if you use JSX.Element instead?
Show answer
Use children: ReactNode, which is required because a modal with no content makes no sense. With JSX.Element, string children and multiple children produce type errors, because a string is not an element and several children arrive as an array.
Exercise
Why does {isOpen && <p>Open</p>} type-check as children: ReactNode but not as children: ReactElement?
Show answer
When isOpen is false, the expression evaluates to false. ReactNode includes booleans, so it accepts that value. ReactElement accepts only element objects, so false is rejected.