A union type (`A | B`) says a value can be either shape. Literal types narrow a primitive down to exact values, and combining literals into a union creates a lightweight enum-like type.
ts
type Chain = "ethereum" | "polygon" | "arbitrum";
function explorerUrl(chain: Chain): string {
if (chain === "ethereum") return "https://etherscan.io";
return `https://${chain}scan.io`;
}TypeScript narrows the type inside `if`/`switch` branches based on comparisons, so inside `if (chain === "ethereum")` the compiler knows `chain` is exactly `"ethereum"`. This is called control-flow narrowing and is central to writing safe union-handling code.