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 type aliases for IDs, timestamps, and emails
- Create
UserandPosttypes that use these building blocks - Create
userSampleandpostSamplevalues and export them (tests will import them by name)
π° Compose larger types from your primitive aliases.