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
index.ts
and:
  1. Create a UserProfile type alias with required and optional properties
  2. Create users with and without the optional properties
  3. Safely access optional properties (check before using!)

Please set the playground first

Loading "Optional Properties"
Loading "Optional Properties"
Login to get access to the exclusive discord channel.
Loading Discord Posts