TypeScript Patterns for Better React Components
summary
Practical patterns for typing React components with TypeScript: shows how discriminated unions replace multiple boolean props, when to prefer string literals or enums, and how Radix-style slot merging and a strict polymorphic implementation let components accept an as prop safely. Explains augmenting forwardRef to keep generics working, prefers ComponentProps/ComponentPropsWithRef over broad JSX.IntrinsicElements, recommends runtime parsing with Zod instead of any or type assertions, and enables noUncheckedIndexedAccess for safer indexing.
AI-written summary · about
chapters (19)
- 0:00Introduction and speaker intro
- 2:20About the speaker
- 3:12History of typing in React
- 8:17Boolean props and unions
- 11:51Custom components vs variants
- 13:35Enhancing components with HTML types
- 16:50React.FunctionComponent type
- 18:01Reusing types and interfaces
- 19:42Compound vs polymorphic components
- 23:53React.forwardRef patterns
- 25:51React.Context with values
- 27:35Overload function components
- 28:25Type vs interface comparison
- 30:15Type annotations and performance
- 30:59Commenting types for productivity
- 32:09Avoiding 'any' and runtime parsing
- 33:40Tooling for cleaner code
- 34:24TypeScript configuration tips
- 34:50Purpose of TypeScript types
