typescript / intermediate
Snippet
Intersection Types
Intersection types combine multiple types into one, allowing you to merge existing types to create a type that has all the features you need.
snippet.ts
1
2
3
4
5
6
7
8
9
type Admin = { privileges: string[] };type User = { name: string };type SuperUser = Admin & User;const boss: SuperUser = {name: "Alice",privileges: ["all"]};
Breakdown
1
Admin & User
The '&' operator creates an intersection, meaning SuperUser must satisfy both Admin and User.
2
const boss: SuperUser
An object of this type must include both 'name' and 'privileges'.