Type Composition
Composition
π¨βπΌ Type aliases can reference other type aliases, letting you build complex
types from simpler ones. This creates a vocabulary for your domain.
type Name = string
type Age = number
type User = { name: Name; age: Age }
Using type aliases for primitives adds semantic meaning. Instead of seeing
string everywhere, you see Email, ID, or Timestampβmaking code
self-documenting.π¨ Open
and:
- Create primitive aliases
ID(string),Timestamp(number), andEmail(string) - Create a
Usertype that uses those primitives forid,createdAt,updatedAt, andemail, plus aname: stringfield - Create a
Posttype that uses those primitives forid,createdAt,updatedAt, andauthorId, plustitleandcontentstring fields - Create
userSampleandpostSamplevalues that match those types and export them by name
Completion criteria
- Named exports:
userSample,postSample userSamplehas stringid/name/emailand numbercreatedAt/updatedAtpostSamplehas stringid/title/content/authorIdand numbercreatedAt/updatedAt
π° Compose larger types from your primitive aliases.