Learning TypeScript: Type Narrowing, Guards, and Assertions

📘 Day 03 of Learning TypeScript — 🚀 Today was all about diving deeper into TypeScript and understanding how TS makes JavaScript safer, cleaner, and smarter. Some major concepts clicked today—especially around type narrowing and type assertions. 🔥 What I Learned Today ✅ 1. Type Narrowing I understood how TypeScript becomes smart based on conditions: 🏹 typeof 🏹 instanceof 🏹 truthiness checks 🏹 switch-case narrowing This helps TS auto-detect whether a value is a string, number, or a specific object type. ✅ 2. Custom Type Guards ✅ 3. Discriminated Union Types Learnt how to create multiple object variations like Masala, Ginger, Elaichi Chai and use the type field for precise narrowing. ✅ 4. Type Assertions (as) Practiced: ✔ Converting any → specific type ✔ Using assertion with JSON.parse() ✔ DOM element type assertion like as HTMLInputElement ✅ 5. Safe Error Handling if (error instanceof Error) to safely access error details. 🧠 Key Takeaway TypeScript isn’t just about types — It’s about making JavaScript predictable, reliable, and professional. Let’s keep going! 💪 #TypeScript #LearningJourney #100DaysOfCode #WebDevelopment #JavaScript #React #DeveloperJourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories