Unions & Intersections

型を「合成」する力。OR(または)と AND(かつ)。

Union Type
「A または B」を表す型。| を使う。
Intersection Type
「A かつ B」を表す型。& を使う。
Literal Type
特定の値だけを許可する型(例: "success" | "error")。
Utility Types
既存の型から新しい型を作る便利な道具(Pick, Omitなど)。

Union Types (|)

OR Logic
// Union Type: A OR B
type Status = "success" | "error" | "loading";
function showStatus(s: Status) {
if (s === "success") {
console.log("Good job!");
}
}
// Error: "pending" is not allowed
// showStatus("pending");

Intersection Types (&)

AND Logic
// Intersection Type: A AND B
type Draggable = { drag: () => void };
type Resizable = { resize: () => void };
type UIElement = Draggable & Resizable;
const box: UIElement = {
drag: () => {},
resize: () => {},
};