Skip to content
dreamcode
dreamcode
Map
Arrays
Lesson 4 of 26
+15 XP on finish
TYPESCRIPTChapter 1 · TS Basics

Typed Arrays and Tuples

Define arrays using type[]. For fixed-length arrays with specific types at exact index positions, use Tuples (e.g. [string, number]).

Worked example

How it reads

  • number[] declares an array containing only numbers
  • [number, number] restricts coordinates to a fixed length of 2 numbers
Cloud tip: Tuples are highly useful for return values like coordinates or key-value pairs.
index.ts
TYPESCRIPT
type-checked, then run in your browser
Console
Run your code to see its output here.
YOUR TURN

Write swap(pair: [string, number]): [number, string] that flips a pair, and log swap(location): [ 450, 'Orion' ].

Press Run to check your work.