TypeScript for React Developers: Types, Props, and Hooks
Add type safety to React with TypeScript—component props, hooks, events, and generic patterns that scale.
TypeScript catches bugs at compile time and improves editor support. Here’s how to use it effectively in React.
TypeScript and React code
Core patterns
Component props — Define an interface or type for props. Use React.FC<Props> sparingly; prefer function MyComponent(props: Props).
Children — Type as React.ReactNode for flexible content. Use React.PropsWithChildren<Props> when you only add children to your props type.
Events — Use React.ChangeEvent<HTMLInputElement>, React.MouseEvent<HTMLButtonElement>, etc. Inline handlers get correct inference.
useState — Type the generic: useState<User | null>(null). TypeScript infers the setter from the initial value when possible.
useRef — For DOM refs use useRef<HTMLInputElement>(null). For mutable values use useRef<number>(0).
TypeScript usage in frontend (survey):
TypeScript in frontend projects (%)
Best practices
Keep prop types in a single place (e.g. types.ts or next to the component). Use satisfies and strict mode. Type API responses and form data so the whole data path is safe.
A short TypeScript + React primer:
Gradual adoption
You can add TypeScript file-by-file. Start with new components and shared types, then tighten tsconfig (strict, noImplicitAny) over time.