Fundamentals4 / 5
Up next

Literal union types

// a union of string literals restricts the allowed values
type Direction = "north" | "south" | "east" | "west";

function step(d: Direction): [number, number] {
  switch (d) {
    case "north":
      return [0, 1];
    case "south":
      return [0, -1];
    case "east":
      return [1, 0];
    case "west":
      return [-1, 0];
  }
}

console.log(step("east"));
What you're solving

a union of literals as an exhaustive input