TypeScript Advanced Types

🚀 TypeScript Advanced Types (Complete Guide)
Advanced Types in TypeScript help you write safer, more expressive, and scalable code.
They are heavily used in real-world apps (React, Node.js) and are common in interviews.
1️⃣ Union Types (|) ⭐
Allow a variable to be one of multiple types.
Union in Functions
2️⃣ Intersection Types (&) ⭐
Combine multiple types into one.
✔ Must satisfy all types
3️⃣ Type Aliases 🔖
Create a custom name for a type.
✔ Improves readability
✔ Reusable
4️⃣ Literal Types ⭐
Restrict values to specific literals.
✔ Very common in APIs & UI state
5️⃣ Type Narrowing 🔍 (Very Important)
TypeScript narrows types using checks.
typeof
in Operator
6️⃣ Discriminated (Tagged) Unions ⭐⭐
A powerful pattern using a common property.
✔ Extremely common in React reducers
7️⃣ unknown vs any ⭐
any ❌ (Avoid)
unknown ✅ (Safe)
✔ Forces type checking
8️⃣ never Type 🔥
Represents values that never occur.
Exhaustiveness Check
✔ Used in advanced safety checks
9️⃣ Optional & Nullable Types ❓
🔟 Type Assertions (as) ⚠️
Tell TypeScript you know better.
⚠️ No runtime checking
1️⃣1️⃣ Mapped Types 🔁
Create new types from existing ones.
1️⃣2️⃣ Conditional Types ⭐⭐
Types based on conditions.
1️⃣3️⃣ Utility Types (Very Important) ⭐⭐⭐
Built-in helpers:
| Utility | Example |
|---|---|
Partial<T> | All optional |
Required<T> | All required |
Readonly<T> | Immutable |
Pick<T, K> | Select keys |
Omit<T, K> | Remove keys |
Record<K, T> | Key-value map |
1️⃣4️⃣ Index Signatures 🔑
1️⃣5️⃣ Advanced Function Types
❌ Common Mistakes
Overusing
anyForgetting type narrowing
Ignoring
neverMisusing type assertions
Not using discriminated unions
📌 Interview Questions (Must Prepare)
Difference between
typeandinterfaceanyvsunknownWhat is a discriminated union?
What is
neverused for?Union vs Intersection types
Utility types examples
✅ Summary
✔ Advanced types improve type safety & scalability
✔ Unions & intersections model real data
✔ Discriminated unions are extremely powerful
✔ Utility & conditional types reduce boilerplate
✔ Essential for React, Node.js & interviews
