Typing React components well means describing the values and interactions a component actually supports, not adding casts until the compiler stops complaining. This checklist shows how to type props, children, DOM events, state, reducers, refs, and forwarded refs in a reusable React and TypeScript workflow.
Overview
A reliable React TypeScript component usually follows a simple order: define the public props, type any children explicitly, infer local state where possible, use event types that match the element, and choose a ref type based on what the ref points to. Keeping those decisions close to the component makes errors easier to understand and prevents implementation details from leaking into its API.
For most components, a type alias or interface is suitable for props. The important choice is not the syntax but the contract. Required values should be required, optional values should have a deliberate fallback, and callback parameters should describe the data the component sends to its parent. If you are deciding between the two declaration styles, see TypeScript Interfaces vs Types.
| Scenario | Recommended pattern | Example |
|---|---|---|
| Component props | Type the public contract | type ButtonProps = { label: string } |
| Children | Use an explicit React node type | children: React.ReactNode |
| DOM events | Match the event to the element | React.ChangeEvent<HTMLInputElement> |
| State | Infer simple values; provide a generic for unions or empty collections | useState<User | null>(null) |
| Reducers | Use discriminated action types | { type: 'reset' } |
| DOM refs | Use a nullable element ref | useRef<HTMLDivElement | null>(null) |
| Forwarded refs | Type the element and the component props together | forwardRef<HTMLButtonElement, Props> |
Checklist by scenario
1. Start with component props
Define the smallest useful public API. Callback props should accept the values the parent needs, rather than exposing a browser event when the component can provide a simpler value.
type SaveButtonProps = {
label?: string;
disabled?: boolean;
onSave: () => void;
};
function SaveButton({
label = 'Save',
disabled = false,
onSave,
}: SaveButtonProps) {
return (
<button type="button" disabled={disabled} onClick={onSave}>
{label}
</button>
);
}
Use optional props only when the component has a sensible default or genuinely supports omission. Avoid making every prop optional to satisfy a caller; that shifts uncertainty into the component.
2. Type children according to what the component renders
React.ReactNode is a practical choice for content that may be text, an element, a fragment, or another renderable value.
type PanelProps = {
title: string;
children: React.ReactNode;
};
function Panel({ title, children }: PanelProps) {
return (
<section aria-labelledby="panel-title">
<h2 id="panel-title">{title}</h2>
<div>{children}</div>
</section>
);
}
Do not use JSX.Element as a universal replacement for React.ReactNode. It is narrower and may reject valid children such as strings or numbers. If a component requires one specific element shape, express that narrower requirement intentionally.
3. Match event handlers to the element
React event types are generic over the element involved. A controlled input commonly uses React.ChangeEvent<HTMLInputElement>, while a form submission uses React.FormEvent<HTMLFormElement>.
function SearchBox() {
const [query, setQuery] = React.useState('');
function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
setQuery(event.currentTarget.value);
}
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
console.log(query);
}
return (
<form onSubmit={handleSubmit}>
<input value={query} onChange={handleChange} />
<button type="submit">Search</button>
</form>
);
}
Prefer currentTarget when you mean the element whose handler is running. The broader target property can refer to a descendant and may not have the properties you expect.
4. Type state at the point where inference needs help
Let TypeScript infer obvious state:
const [isOpen, setIsOpen] = React.useState(false);
const [name, setName] = React.useState('');
Provide a type argument when the initial value does not describe the eventual state:
type User = { id: string; name: string };
const [user, setUser] = React.useState<User | null>(null);
const [items, setItems] = React.useState<string[]>([]);
For state with several meaningful statuses, a discriminated union is often clearer than several booleans. Type narrowing can then make each render branch safe; the TypeScript type narrowing guide covers this pattern in more detail.
5. Use discriminated actions with reducers
Reducers benefit from an action union whose type field identifies each payload.
type State = { count: number };
type Action =
| { type: 'increment' }
| { type: 'add'; amount: number }
| { type: 'reset' };
function reducer(state: State, action: Action): State {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'add':
return { count: state.count + action.amount };
case 'reset':
return { count: 0 };
}
}
const [state, dispatch] = React.useReducer(reducer, { count: 0 });
When a new action is added, the switch becomes a useful place for the compiler to expose incomplete handling. Avoid typing actions as a broad object such as { type: string }; that removes the relationship between an action and its payload.
6. Choose the correct ref pattern
A DOM ref starts as null and later points to an element, so include both possibilities:
const inputRef = React.useRef<HTMLInputElement | null>(null);
function focusInput() {
inputRef.current?.focus();
}
For a mutable non-DOM value, use the value type instead, such as React.useRef<number | undefined>(undefined). Do not use a DOM element type for timers, cached values, or instance-like data.
When forwarding a ref, type both the exposed element and the component props:
type TextInputProps = React.ComponentPropsWithoutRef<'input'> & {
label: string;
};
const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
({ label, ...inputProps }, ref) => (
<label>
{label}
<input ref={ref} {...inputProps} />
</label>
)
);
TextInput.displayName = 'TextInput';
This approach reuses the intrinsic input attributes while adding the component's own required prop. Use equivalent helpers for other elements when appropriate.
What to double-check
- JSX configuration: Confirm the project's JSX setting and framework tooling agree. A type error can come from configuration rather than the component itself.
- React type definitions: Check that the React and related type packages are compatible with the runtime and build setup. After an upgrade, inspect changed errors before adding assertions.
- Element specificity: Use
HTMLTextAreaElement,HTMLSelectElement, or another precise element type instead of defaulting toHTMLElement. - Nullability: Handle an absent ref or data value with a guard, optional chaining, or an explicit loading branch. Do not silence the error with
asunless the invariant is enforced elsewhere. - Prop composition: Decide whether a wrapper should accept all native attributes.
ComponentPropsWithoutRefcan help, but exposing every attribute may make a small component's API harder to control. - Compiler strictness: Test the component under the repository's actual
tsconfig, not an isolated editor setting. Strict mode often reveals useful issues in callbacks, refs, and nullable state.
When a type must reflect a known object without weakening its inferred literals, consider the satisfies and as const comparison. Keep assertions at boundaries and prefer narrowing inside the component.
Common mistakes
- Typing every handler as a generic function:
(event: Event) => voidoften loses React and element-specific information. Infer the handler from the JSX prop or use the matching React event type. - Using
anyfor difficult props: This hides the contract from both the component and its callers. Start with a small union or an unknown value followed by validation. - Confusing event and value callbacks: A reusable
Selectcomponent can exposeonValueChange(value: string)instead of forcing every parent to understand its internal event. - Overusing
React.FC: A normal function declaration makes props, return behavior, and children expectations explicit. Use the style that fits the codebase, but do not rely on a component helper to define an unclear API. - Forcing a non-null ref: A ref is commonly null during initial render and after unmount. Model that lifecycle rather than asserting that
currentalways exists. - Mixing native and custom props carelessly: A custom prop can collide with a native attribute or create confusing spread order. Name and merge props deliberately.
When to revisit
Return to this checklist whenever you add a component, introduce a form, expose a reusable callback, or troubleshoot an event or ref error. It is especially useful before a planned dependency or framework upgrade: verify the JSX configuration, review React type-definition changes, run the project's type check, and inspect any new nullability or event errors without immediately applying casts.
For an existing codebase, make the review practical:
- Open the component's public props and remove unused or overly broad fields.
- Check every event handler against the actual element that emits it.
- Mark asynchronous and initially empty state as nullable or explicitly loading.
- Confirm reducer actions are a discriminated union and that all cases are handled.
- Verify each ref's element type and whether the ref is local, forwarded, or mutable data.
- Run the repository's formatter, linter, tests, and TypeScript build after the change.
These checks keep types aligned with runtime behavior. If a problem crosses component boundaries, compare it with the React forms with TypeScript guide or the project’s broader strict-mode migration plan before changing the component contract.