Typing Functional Components and Props in TypeScript
Typing Functional Components and Props in TypeScript
TypeScript is the foundation of enterprise React development. Typing functional components and their props guarantees compile-time type safety, eliminates undefined is not a function runtime errors, and provides rich autocompletion across design systems.
1. Modern Component Typing: Direct Interface vs React.FC
In early TypeScript React, React.FC (or React.FunctionComponent) was common. However, modern enterprise codebases prefer direct prop typing:
Why React.FC has Fallen Out of Favor
- In React 17 and earlier,
React.FCimplicitly injectedchildren: ReactNodeinto every component, even if the component was a leaf node (like a self-closing badge or button). - It complicates generics:
<T>(props: Props<T>)is awkward to write withReact.FC. - Direct function typing is cleaner and mirrors standard TypeScript functions.
2. Typing children Explicitly
When a component accepts nested children, define children explicitly using React.ReactNode:
3. Extending Native HTML Attributes
When building design system primitives (like custom buttons or text fields), extend native HTML attributes so your components accept standard attributes (id, aria-label, disabled, style, className) automatically:
Practice Quiz
Typing React Hooks, Generics, and Refs
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Memory Profiling and Heap Snapshots in Production | Typing React Hooks, Generics, and Refs |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.