Intersection Types
Intro to Intersection Types
Intersection types combine multiple types into one. A value must satisfy all
the combined types.
type A = { name: string }
type B = { age: number }
type C = A & B // { name: string; age: number }
Intersection vs Union
- Union (
|) - Value is ONE of the types - Intersection (
&) - Value is ALL of the types
type StringOrNumber = string | number // One or the other
type Named = { name: string }
type Aged = { age: number }
type Person = Named & Aged // Has both name AND age
Combining Behaviors
Intersections are great for composing types:
type Timestamped = { createdAt: Date; updatedAt: Date }
type Authored = { authorId: string }
type Post = { title: string; content: string } & Timestamped & Authored
// Post has title, content, createdAt, updatedAt, AND authorId
With Type Aliases
type BaseEntity = { id: string }
type User = BaseEntity & { name: string; email: string }
// User has id, name, and email
Intersection types are an alternative to interface extension. They're more
flexible because they work with any types, not just interfaces.
In this exercise, you'll use intersections to compose complex types.