Arrays, Tuples, and Literal Types

Use T[] or Array<T> for arrays whose elements share a type. A tuple describes a fixed positional structure, such as [number, string]; optional and rest elements can model more flexible tuples. Literal types restrict values to specific strings, numbers, booleans, or unique symbols, and are often used for modes and discriminants.

as const preserves literal values and makes object properties and arrays readonly at the type level. It does not freeze the object at runtime. Prefer unions of literal values over a generic string when only a small known set is valid.

Array, tuple, and literal union
const scores: number[] = [82, 91, 76];
const userRecord: [id: number, name: string] = [7, "Mira"];

type Theme = "light" | "dark" | "system";
const theme: Theme = "dark";
💡 scores is an array of numbers, userRecord has two typed positions, and Theme permits only the three declared strings.
Key Takeaways
  • Arrays describe repeated values; tuples describe positions.
  • Literal unions constrain a value to known alternatives.
  • as const affects compile-time inference, not runtime immutability.
Knowledge Check

Which type best models a value that can only be `"small"`, `"medium"`, or `"large"`?