100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
TypeScript Deep Dive
30 minintermediate

Arrays, Tuples and Enums

Real programs rarely deal in single values; they deal in collections and fixed sets of choices. TypeScript gives three precise tools for these: arrays for ordered lists of the same type, tuples for fixed-length sequences where each position has its own meaning, and enums for a closed set of named constants. Choosing the right one matters because each communicates a different intent to both the compiler and the next developer. An array says 'any number of these'; a tuple says 'exactly these, in this order, with these meanings'; an enum says 'one of this fixed set of named options.' Without these distinctions, developers fall back on loosely-typed arrays and bare strings, losing the compiler's ability to catch a swapped argument, an out-of-range option, or a malformed pair. Mastering when to reach for each is what turns a vaguely-typed collection into a self-documenting, self-checking structure.

Analogy🏏Cricket
🏏 Think of it like cricket: Picture how a scoreboard operator constructs every display string from fixed components in a strict format — a batter's line is always built as `${name} ${runs} (${balls})`, never improvised. The format is a template with slots, and only values that fit each slot are allowed. Just as the scoreboard builds each line by filling a fixed template with the right pieces, a template literal type builds a string type by filling a pattern with other types. Just as a stray entry that does not match the format — text where runs should go — is rejected by the operator, a string that does not match the template literal type is rejected by the compiler. Just as the format guarantees every line is readable and consistent, the template type guarantees every string matches the intended shape. This reveals why template literal types matter: strings in software follow patterns just like scoreboard lines, and encoding the pattern into the type makes every malformed string an immediate error.
Lesson 3 of 35
0% complete