Optional chaining with types
📖 Scenario: Imagine you have a list of users, and each user may or may not have a profile with an address. You want to safely get the city name without causing errors if some data is missing.
🎯 Goal: Build a TypeScript program that uses optional chaining to safely access nested properties in user objects.
📋 What You'll Learn
Create an array of user objects with optional nested properties
Add a variable to hold the index of the user to check
Use optional chaining to access the city name safely
Print the city name or 'Unknown' if it does not exist
💡 Why This Matters
🌍 Real World
Optional chaining helps avoid errors when working with data that may be incomplete or missing, such as user profiles or API responses.
💼 Career
Many jobs require handling uncertain data safely in TypeScript, making optional chaining a valuable skill for frontend and backend developers.
Progress0 / 4 steps