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 type alias named
UserProfilewith:- required:
name(string),email(string) - optional:
bio(string),website(string)
- required:
- Type the provided
aliceandbobobjects asUserProfile- Keep
alicewithoutbioorwebsite(they should be omitted) - Keep
bobwith a non-emptybiostring and awebsitethat starts withhttp
- Keep
- Create a
displayUserInfofunction that logs name, email, and safely handles missingbio/website(for example with??defaults) - Export
aliceandbob(grading checks these two exports)
You'll know you're done when
alice has only the required fields and bob
includes both optional fields.

