Optional Properties
Optional Properties
π¨βπΌ Not every user fills out all their profile fields. We need to handle
optional data gracefully.
Use
? to mark a property as optional:type UserProfile = { name: string; bio?: string }
const user: UserProfile = {
name: 'Alice',
// bio is optional, so we can omit it
}
π¨ Open
and:
- Create a
UserProfiletype alias with required and optional properties - Create users with and without the optional properties
- Safely access optional properties (check before using!)