2025-05-10 Web Development, Programming
🧠 Understanding TypeScript Generics — With a React Example
By O. Wolfson
Generics in TypeScript are a way to make components, functions, or types more reusable and type-safe. They allow you to defer specifying a type until later, so your code can adapt to different shapes while still maintaining strict typing.
🧩 Why Use Generics?
Generics are useful when:
- You want to reuse code for multiple data types
- You want strong type safety without duplicating logic
- You need to preserve relationships between inputs and outputs
Without generics, you'd either have to:
- Write the same code multiple times for different types
- Use
any
and lose type safety
📦 Real-World Example: A Typed Input With Validation
Let's create a reusable <TypedInput />
component that accepts a label, a value, and a validation function — and uses generics to remain flexible about what type the input holds.
It will show a validation error using ShadCN's Alert
component when validation fails.
User Input
🧱 Component Code: TypedInput.tsx
🧪 Usage Example
🔍 What Makes This Work?
TypedInput<T>
is generic overT
- It works for both
number
andstring
, but the logic is reused - Validation logic is type-aware
- No
any
, no type duplication, just safe reuse
✅ Summary
Without Generics | With Generics |
---|---|
Multiple components (NumberInput , TextInput ) | One TypedInput<T> |
Repeated validation logic | Reusable validation pattern |
any everywhere | Type-safe, inferred values |
🧬 Anatomy of a Generic in This Component
Let's break down how generics power TypedInput<T>
:
T
is a type parameter — it stands in for whatever type you specify when you use the component (number
,string
, etc.).TypedInputProps<T>
usesT
to type the props:initialValue
,validate()
, andonChange()
. This ensures all values stay consistent.
- The function is generic, and TypeScript enforces that the same
T
flows through the props and logic. - Inside
handleChange
, it infers how to coerceraw
into aT
usingtypeof initialValue
.
- When used, the component becomes typed:
T = number
orT = string
. This gives full autocomplete, validation, and inference — noany
, no duplication.
🧠 Think of generics like templates for types — they let you write code once, and fill in the types later.
Generics make your components reusable, predictable, and scalable. When you need flexibility without sacrificing type safety, generics are the way forward.