A polymorphic component is an advanced design system component that can dynamically change its underlying HTML element or React component via an as prop while retaining strict TypeScript type safety. For example, a <Button as="a" href="/login"> renders an <a> tag and permits href, while <Button as="button" type="submit"> forbids href and permits type.
Without polymorphism, design system developers often duplicate code: creating <Button>, <LinkButton>, <DivButton>.
Naive typing using React.ElementType fails because TypeScript cannot dynamically validate that an href prop is only legal when as="a", or that target is illegal when as="button".
<Button> Componentref Support with Polymorphic forwardRefTo forward refs safely across polymorphic elements:
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Typing Synthetic Events and Form Elements | JWT Authentication Flow and Silent Token Refresh |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.