Keep a task and its minutes together in a tuple: an array with a fixed number of places, each with a type of its own. Write the type as [string, number], read each place by its index, and sort, search and add up a whole list of pairs without a task ever losing its minutes. See why TypeScript makes a plain list unless you write the tuple’s type, and what TS2493 means when you count places from 1.
Fundamentals
TypeScript
Simple tuples. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
The last lesson added up the minutes of today's tasks: 25, 15, 30
and 10. Say the tasks themselves live in a second list, tasks, in
the same order, so tasks[1] and minutes[1] are the same task. Now
you want to start with the shortest one. minutes.toSorted(…) from
lesson 4.4 gives 10, 15, 25 and 30, and tasks still begins with
"buy milk". Which task takes 10 minutes? The sorted list cannot
say. The minutes have left their tasks behind.
Think of the price tags in a shop. Each tag carries a product's name
and its price, always on the same tag, the name on top and the price
under it. A shop that kept every name on one list and every price on
another would cope until someone tidied the price list from cheapest
to most expensive; after that, nobody could say what costs what. By the end
of this lesson you keep a task and its minutes together in one
value, a tuple, write its type, read each place by its index, and
sort a whole list of them without a task ever losing its minutes.
It prints buy milk 10, and buying milk takes 25 minutes, not 10.
TypeScript finds nothing wrong, and Node runs it without a word. The
two lists are joined only by a promise you keep in your head: the same
index means the same task. toSorted keeps that promise for the one
list it sorts and knows nothing about the other. Add a task to one
list and forget the other, and the promise breaks the same way.
What you want is one value per task that holds both things at once:
the task, and its minutes, side by side, so that wherever one goes,
the other goes too.
A tuple: one place for each thing
A tuple is an array with a fixed number of places, where each
place has a type of its own. You write it like any array, in square
brackets. Its type is written in square brackets too, with one type
for each place, in order:
It prints buy milk, then 30, then 2. Read [string, number] as
"two places: a string, then a number". Set it beside string[] from
lesson 4.1: brackets after a type mean a list of any length, all of
that one type; brackets around the types mean places, one for each
type inside. Hover over the 1 of entry[1] in VS Code (Visual
Studio Code, your editor) and it says number, so + 5 adds; over
entry[0] it says string.
Each place is still read by its index, counted from 0, as in every
list. The grid below is drawn the way lesson 4.1 drew tasks, with
a column for the type added:
What is thereIts typeentry[0]
"buymilk"
The task
string
Text, like any string
entry[1]
25
Its minutes
number
A number you can add to
entry[2]
The last row is new. A tuple's type says how many places it has, so
TypeScript refuses index 2 before anything runs, with TS2493 (TS
for TypeScript, then the error's number); the common mistake below
shows it.
The order of the places counts as much as their number. Written the
other way round, [25, "buy milk"] is refused twice with TS2322,
once for each place:
Type 'number' is not assignable to type 'string'. under the 25, and
the same with the two types swapped under the text.
A list of pairs
One tuple holds one task. A tuple of two places is often called a
pair. A day holds several, so put the pairs in a list. Its type is
the tuple's type followed by [], the way string[] follows
string:
Read [string, number][] from the inside out: [string, number] is
one pair, and [] makes it a list of them. Each item of plan is a
whole pair: plan[1] is ["call mom", 15]. Nothing here is a new
kind of list. Every method from this module works on it as before;
only the items are pairs now.
So a task can no longer lose its minutes. A method hands its
rule one item at a time, and here an item is a task together with
its minutes, so they cannot come apart. The rule reads the place it
needs by its index. Press Next step to walk the belt, or let it
play, and switch between map and find:
plan
["buy milk", 25]
["call mom", 15]
["pay rent", 30]
["call dad", 10]
.map((entry) => entry[1])["buy milk", 25]→gives25["call mom", 15]→gives15["pay rent", 30]→gives30["call dad", 10]→gives10One item at a time
minutes
25
15
30
10
Each pair rides the belt as one item; the rule reads entry[1]
All 6 steps shown.Step 1 of 6: plan.map((entry) => entry[1])Step 2 of 6: ["buy milk", 25] → 25Step 3 of 6: ["call mom", 15] → 15Step 4 of 6: ["pay rent", 30] → 30Step 5 of 6: ["call dad", 10] → 10Step 6 of 6: minutes = [25, 15, 30, 10]
All 6 steps shown.
plan
["buy milk", 25]
["call mom", 15]
["pay rent", 30]: never reached
["call dad", 10]: never reached
.find((entry) => entry[1] < 20)["buy milk", 25]→givesfalse["call mom", 15]→givestrue: found, stops["pay rent", 30]→givesfalse["call dad", 10]→givestrueOne item at a time
firstQuick
["call mom", 15]
Each pair rides the belt as one item; the rule reads entry[1]
All 4 steps shown.Step 1 of 4: plan.find((entry) => entry[1] < 20)Step 2 of 4: ["buy milk", 25] → falseStep 3 of 4: ["call mom", 15] → true: found, stopsStep 4 of 4: firstQuick = ["call mom", 15]
All 4 steps shown.
map takes one place out of every pair and makes a plain list of
numbers. find gives back the first pair its rule says true to,
whole: ["call mom", 15], task and minutes together. As in lesson
4.3, find may find nothing, so its type is
[string, number] | undefined, and you check it before you use it.
Sorting works the same way. The compare function of lesson 4.4 gets
two items, a and b, and here each is a pair, so a[1] - b[1]
compares their minutes. The pairs move as wholes. Press Step to
watch the rule's line run each time toSorted asks about two pairs,
or Run to see the plan from the shortest task up:
It prints call dad: 10 minutes first and pay rent: 30 minutes
last, and every task keeps its own minutes. The rule is on a line of
its own only so that the player stops on it each time.
TypeScript does not infer a tuple
Leave the type out, and something quiet happens. Hover over entry in
const entry = ["buy milk", 25]; and VS Code says
(string | number)[]: a list of any length, where each item is a
string or a number. The | reads "or", as in lesson 3.3, which joined
a type with null or undefined; here it joins two ordinary types.
The parentheses around string | number keep the two together (they
are not a tuple's square brackets), and [] after them makes a list.
TypeScript cannot know that you mean "exactly two, a string then a
number". Lesson 4.1 showed that a list can grow with push, so from
the brackets alone it picks the type that fits any list made of
these items. A tuple is a promise only you can make, and you make it
by writing the type. Here is what that one change does to the same
code:
No type written
Does: Makes a list of any length, not a tuple
Never: Never knows which place holds the number
[string, number] written
Does: Place 0 is a string, place 1 a number
The same code, with the tuple's type and without it
The refusal in the first version, with no type written, is TS2362.
The same happens to a whole list of pairs when you add up their
minutes with reduce from lesson 4.5:
Bad — no type on the list, so each
pair is a (string | number)[] and entry[1] might be text.
Good — [string, number][], so
entry[1] is a number in every pair.
The first is refused with
Operator '+' cannot be applied to types 'number' and 'string | number'.
(TS2365), with the ~ under total + entry[1], five lines below
the list that caused it. Whenever you mean a tuple, write its type on
the line that makes it: [string, number] for one,
[string, number][] for a list of them.
Predict before you run
This time the plan is sorted the other way round. Decide what the
last line prints.
Predict
What does this program print?
The common mistake: counting places from 1
People count from 1, so the second place feels like number 2. In a
list of a hundred tasks that slip goes unnoticed. In a tuple of two,
it lands right past the end.
Common mistake
Reading the minutes at index 2
You want to print call mom: 15 minutes, so you reach for the first
place and the second place, counted the way you count:
npx tsc --noEmit--types node today.ts (tsc is the TypeScript
compiler, started by npx) refuses it before it runs:
The first line is broken here to fit the page.
Tuple type '[string, number]' of length '2' has no element at index '2'.
The fix
Count the places from 0, as every index in this module does: the
task is entry[0], the minutes entry[1].
Read it with the five questions from lesson 1.5: TypeScript wrote it,
on line 2, and the ~ stands under the 2 inside entry[2]. Run it
anyway with node today.ts: Node never runs the checker, so it prints
15: undefined minutes, the quiet undefined of lesson 4.1. When you
see TS2493, count the places again from 0: the last one is always at
length - 1, which for a pair is 1.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
Each exercise is checked in your browser. Press Check when you
think you have it; if you are stuck, the hints come one at a time.
Recap
Recap · 3 questions
Check yourself
What does the type [string, number] promise?
Check yourself
What type does const entry = ["buy milk", 25] get?
Check yourself
What does entry[2] do on a [string, number]?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Simple tuples
What things are
Word
What it means
Tuple
An array with a fixed number of places, a type for each
Place
One position in a tuple, read by its index
[string, number]
Two places: a string, then a number
[string, number][]
A list of such pairs
Code
Write
To
const entry: [string, number] = ["buy milk", 25];
Make a tuple
entry[0]
Read the first place, the task
entry[1]
Read the second place, the minutes
plan.toSorted((a, b) => a[1] - b[1])
Sort pairs by their minutes
plan.find((entry) => entry[1] < 20)
Get a whole pair, or undefined
total + entry[1]
In reduce: add up every pair's minutes
Rules
Rule
What it means
Write the type
Without it, a list: (string | number)[]
Order counts
[25, "buy milk"] is TS2322
Count from 0
A pair's last place is 1
Past the end
entry[2] is TS2493, refused before it runs
Pairs move whole
Sorting pairs keeps each task with its minutes
Never push
TypeScript lets it through; the type stays at two
Where to go next
You can now keep a task and its minutes together in a tuple, write
its type as [string, number], read each place by its index, and
sort, search and add up a whole list of pairs without a task ever
losing its minutes. You know why the type has to be written, and
what TS2493 means when you meet it.
Next is Reading code: tracing a loop by hand. All module long
the player has shown you what each box holds after every line.
There you do that yourself, turn by turn, so you can read a loop
you cannot run.
Before you move on, go back to plan.ts, press Edit, add a
fifth task, ["walk dog", 5], and predict where toSorted puts it
before you press Run.
No
place
The tuple ends at 1
TS2493
Refused before it runs
entry has two places, each with a type of its own
Exercise · Read the code
Read the type of a place
What type does TypeScript give entry[1] here?
Outputtext
today.ts:2:35 - error TS2493: Tuple type '[string, number]' of length'2' has no element at index '2'.2 console.log(`${entry[1]}: ${entry[2]} minutes`); ~Found 1 error in today.ts:2