TypeScript with React Cheatsheet
Learn how to use TypeScript in React for typed components, props, and state.
· 8 min read · AI-reviewed
Learn how to use TypeScript in React for typed components, props, and state.
Source: z2h.fyi/cheatsheets/typescript-react — Zero to Hero cheatsheets for developers.
Combine TypeScript’s static typing with React’s component model to catch bugs early and improve IDE support.
npm i -D typescript @types/react @types/react-dom.tsconfig.json has "jsx": "react-jsx".| Concept | Description |
|---|---|
| Props Interface | Define shape of component props. |
| State Types | Type the state object in useState. |
| FC vs. Function | Use React.FC<Props> for functional components. |
| Generics in Hooks | Type generic data fetching hooks. |
interface Props { title: string; count?: number; }const MyComp: React.FC<Props> = ({title, count = 0}) => {...}const [value, setValue] = useState<string>('');function useFetch<T>(url: string): { data: T | null; error: Error | null } { … }handleChange<T> that updates form state.any; prefer unknown or generic constraints.as const for literal props to preserve exact types.