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 type alias named UserProfile with:
    • required: name (string), email (string)
    • optional: bio (string), website (string)
  2. Type the provided alice and bob objects as UserProfile
    • Keep alice without bio or website (they should be omitted)
    • Keep bob with a non-empty bio string and a website that starts with http
  3. Create a displayUserInfo function that logs name, email, and safely handles missing bio / website (for example with ?? defaults)
  4. Export alice and bob (grading checks these two exports)
You'll know you're done when alice has only the required fields and bob includes both optional fields.

Please set the playground first

Loading "Optional Properties"
Loading "Optional Properties"