Unions represent “either/or”. Intersections combine shapes into “both”. Use unions for finite states and API responses; pair with narrowing. Use intersections to compose behaviors, but avoid conflicting fields.
type Status = "idle" | "loading" | "success" | "error";
type AdminUser = User & { permissions: string[] };type Status = "idle" | "loading" | "success" | "error";
type ApiSuccess = { status: "success"; data: string };
type ApiError = { status: "error"; message: string };
type ApiResponse = ApiSuccess | ApiError;
function handle(res: ApiResponse) {
if (res.status === "success") {
console.log(res.data);
} else {
console.error(res.message);
}
}
type Timestamped = { createdAt: Date };
type Identified = { id: string };
type Entity = Timestamped & Identified;Understanding Unions & Intersections is fundamental to mastering TypeScript. Practice with the examples above and experiment with variations to solidify your knowledge.