Lesson 13 / 26

Literal Types

A type that allows only specific, exact values.

Exact allowed values

A literal union restricts a value to a fixed set of strings or numbers — like a lightweight enum.

type Direction = "up" | "down" | "left" | "right";

function move(dir: Direction) { /* ... */ }
move("up");      // OK
move("diagonal");
// Error: Argument of type '"diagonal"' is not assignable to type 'Direction'.

const and literal widening

let x = "up" infers as string, but const x = "up" infers as the literal "up"const can't be reassigned, so TS keeps the narrower type.