Arrays and Tuples
An array is a list of values that share one type.
A tuple is a short list with a fixed length.
Each spot in a tuple has its own type.
Think of a pair like [name, age].
Typed arrays
Write number[] or Array<number>.
Both mean a list of numbers.
Pushing the wrong type is a compile error.
Compile error means TypeScript stops you before the code runs.
Readonly arrays
When a list should not grow or change, use readonly.
Readonly means look but do not change.
Callers cannot push, pop, or replace items.
Tuples
A tuple type lists each slot.
Order matters.
The first item is one type. The second is another.
Destructuring works nicely with tuples.
Destructuring means pull the pieces out into names.
Optional and rest tuple slots
A tuple can mark the last slot as optional.
Optional means it may be missing.
It can also gather leftover items with a rest type.
Rest means take the remaining pieces into a list.
When to pick which
Use arrays for open lists of one type.
Open means the length can grow.
Use tuples when position and length matter.
Like a fixed pair or a fixed triple.
Use arrays for open lists of one type. Use tuples when position and length matter.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.