Docs
/
TypeScript
Chapter 3
03 — Arrays, Tuples & Enums
Core Concepts
- Typed Arrays —
string[]orArray<string>syntax - Readonly Arrays —
readonly string[]orReadonlyArray<string> - Tuples — fixed-length arrays with specific types per position
- Labeled Tuples — tuples with named elements for readability
- Numeric Enums — auto-incrementing numeric values
- String Enums — explicit string values (preferred)
- const Enums — inlined at compile time, no runtime object
- Enum alternatives —
as constobjects (often preferred over enums)
Array vs Tuple
| Feature | Array | Tuple |
|---|---|---|
| Length | Variable | Fixed |
| Element types | All same | Can differ per position |
| Push/pop | Yes | Allowed but defeats purpose |
| Destructuring | Typed uniformly | Each position typed |
Enum Best Practices
- Prefer string enums over numeric (readable, debuggable)
- Consider
as constobjects instead of enums (tree-shakeable, no runtime overhead) - Use const enum only if bundle size matters and you accept the trade-offs