Five small tasks, one for each skill Level 2 builds on, done with every lesson closed: plan with small functions, fix a failing test, read a type error, check data from a web API, and set up packages safely. Learn why rereading feels like knowing, why edge cases come every time, and where to go back for a miss.
TypeScript
Fundamentals
Self-assessment: are you ready for Level 2?. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
For two years a friend has driven you to the same lake. You know the
way: the bridge, the petrol station, the left turn after the farm.
Then one Saturday you drive there yourself, and at the farm you are
not sure whether it was the first left or the second. You saw that
road every time. You never had to choose a turn on it.
Level 1 is behind you, and Level 2 takes for granted that you can
find the way yourself. This lesson is that drive: five small tasks,
one for each skill Level 2 needs, that you do with every lesson
closed. By the end you know which of the five are yours, and for each
one that is not yet, the lesson to go back to.
What Level 2 builds on
Level 1 promised five things: to break a problem into steps, write
and test small programs, read error messages, work with files and web
APIs (application programming interfaces: websites that answer
programs with data), and use npm packages safely. Level 2 builds on
them rather than teaching them again. Its project, a command-line
client for a public web API, uses all five:
Skill
In Level 2's project
Taught in
Break a problem into steps
One function for each job
5.7
Write and test small programs
A test for every part
5.11
Read error messages
Longer type errors, read to the end
1.5, 8.1
Work with files and web APIs
Asks an API, saves its answers
12.2–12.5
Use npm packages safely
New libraries, from a lockfile
9.3, 14.2
A gap in one of these does not stop you on the first page of Level 2.
It stops you in the middle of something new, where it is hard to
tell which part you are missing. Found today, it costs one lesson.
Seeing it is not writing it
A self-assessment is a check you run on yourself. Not "have I
read this?" but "can I do this now, with nothing open?". Rereading
cannot answer it, because it only tests recognising: knowing a
line when you see it. When you read a lesson, every line arrives with
its explanation, so each one makes sense, and making sense feels like
knowing. A blank file takes the explanation away and tests
writing: making the line yourself. Here are three lines of the
kind Module 11 taught, as you read them in a lesson and as written
from memory:
load.ts
const list = await loadTasks();console.log(list.length);
Reading a lesson
Does: Every line makes sense as you read it
Never: It never asks you to write one
load.ts
const list = loadTasks();console.log(list.length);
Did you forget to use 'await'?
Writing it from a blank file
Does: Shows what you can write without help
Never: It never fills a gap for you
The line you read is not yet a line you can write
The await was in the code you read, and you understood it there.
The blank file shows that you do not reach for it yourself yet. That
is not a failure. It is exactly what you came to find out, and it
costs one red line today instead of an evening lost in Level 2.
A self-check, step by step
Each check is one small task and four steps. Close the lesson and the
cheat sheet. Write the answer from the task alone, in a new file or
in the page's own editor. Run it. Then give it the inputs the task
did not show: an empty list, a 0, a value that is missing. An input
like that, at the edge of what the task allows, is an edge case,
and it is where an answer that looked finished usually breaks.
Pick one of the three endings above the figure, Ready, Stuck
or Breaks on []. In the third, the task was an average, and an
empty list breaks it with NaN, "not a number" (lesson 2.5); the
common mistake below shows that answer.
Press Next step to walk through each ending, or let it play:
Lesson closed: reachedNo cheat sheet open
Blank file: passes✓Write it from the task alone
Run it: passes✓It prints what the task asks
Edge cases: passes✓An empty list, a 0, a missing value
Next skill: reachedThis one is yours
An answer passes every check before you call the skill
yours
All 8 steps shown.Step 1 of 8: Lesson closedStep 2 of 8: Blank fileStep 3 of 8: Blank file: passesStep 4 of 8: Run itStep 5 of 8: Run it: passesStep 6 of 8: Edge casesStep 7 of 8: Edge cases: passesStep 8 of 8: Next skill
All 8 steps shown.
Lesson closed: reachedNo cheat sheet open
Blank file: stops here✕Write it from the task alone
Run it: never reachedIt prints what the task asks
Edge cases: never reachedAn empty list, a 0, a missing value
Next skill: never reachedThis one is yours
Stuck: back to the lessonNo next line without the lesson: back to it
An answer passes every check before you call the skill
yours
All 3 steps shown.Step 1 of 3: Lesson closedStep 2 of 3: Blank fileStep 3 of 3: Blank file: stops here. No next line without the lesson: back to it
All 3 steps shown.
Lesson closed: reachedNo cheat sheet open
Blank file: passes✓Write it from the task alone
Run it: passes✓It prints what the task asks
Edge cases: stops here✕An empty list, a 0, a missing value
Next skill: never reachedThis one is yours
It breaks hereAverage: NaN minutes
An answer passes every check before you call the skill
yours
All 7 steps shown.Step 1 of 7: Lesson closedStep 2 of 7: Blank fileStep 3 of 7: Blank file: passesStep 4 of 7: Run itStep 5 of 7: Run it: passesStep 6 of 7: Edge casesStep 7 of 7: Edge cases: stops here. Average: NaN minutes
All 7 steps shown.
Here is one task, done the whole way. A to-do list should say how
many minutes of work are left today, counting only the tasks not
done yet. First the plan in words, as in lesson 5.7: keep the tasks
that are not done, then add up their minutes. Then the code, run once
with the task's list and once with the edge case, an empty list:
35 is right for the task's list: 20 for the call and 15 for the
milk, and the rent is done. 0 is right for the empty list: with no
tasks there is nothing to add, and reduce gives back its start
value. This answer passes every step.
When a check stops, it tells you where to go. Stuck at the blank file
means the skill is not yours yet: open the lesson behind it (the
table above names it), do its exercises again without looking at the
solutions, and try the task again a day later, from a blank file. A
break at the edge cases is closer: fix the answer for that input,
then run every input again.
Predict before you run
Reading code and saying what it prints is the other half of writing
it. These six lines use three ideas from three different modules.
Write your answer first; then the block's Run appears, and it
waits for the timer as Node does:
A task usually shows one example, and an answer that prints the right
thing for it feels finished.
Common mistake
Checking an answer only with the task's own example
The task: print the average length of a list of tasks, in whole
minutes. Its example is two tasks of 20 and 15 minutes, and your
answer prints Average: 18 minutes for it. Then you try the edge
case, an empty list:
For the empty list, total is 0 and tasks.length is 0, and 0 / 0
is NaN (lesson 2.5). TypeScript cannot catch it,
because every value here is a number, and NaN is a number too.
Average: NaN minutes
The fix
Answer the empty list before you divide:
average.tsts
interface Task { title: string; minutes: number; done: boolean;}function averageMinutes(tasks: Task[]): number { // No tasks, nothing to wait for: answer before dividing by 0 if (tasks.length === 0) { return 0; } const total = tasks.reduce((sum, task) => sum + task.minutes, 0); return Math.round(total / tasks.length);}const today: Task[] = [ { title: "Call mom", minutes: 20, done: false }, { title: "Buy milk", minutes: 15, done: false },];console.log(`Average: ${averageMinutes(today)} minutes`);console.log(`Average: ${averageMinutes([])} minutes`);
The task's list still prints Average: 18 minutes, and the empty
list now prints Average: 0 minutes.
Make the edge cases part of every check, not something you try only
at the end: an empty list, a 0, a value that is missing, and, for
data from outside, a value that is not the type you wrote. A test
from lesson 5.11 is the place to keep each one, so it runs again
every time you change the code.
Pass all five tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
One task per skill, in the order of the table at the top. Close every
other lesson and every cheat sheet first; the page's editor is your
blank file. A task you pass without opening a hint is a skill you
have. One that needed only the first hint is nearly yours: do it again
tomorrow, from a blank file. A task that needed the second hint counts
as a miss, and that hint names the lesson to go back to.
Exercise · Write the code
Plan what fits in your free minutes
Write two small functions, one job each. openTasks(tasks) gives
back the tasks not done yet. planFor(tasks, minutesFree) goes
through what openTasks gives back, in order. It takes a task when
the task fits in the minutes left, and takes its minutes away. A task
that does not fit is skipped, and a later one may still fit. For the
list below and 30 free minutes, the answer is
["Call mom", "Water plants"].
Loading the editor…
Exercise · Fix the error
Make the test pass
The percentDone function from lesson 5.11 has a small mistake in it.
Its test in score.test.ts checks it:
score.test.tsts
import { test } from "node:test";import assert from "node:assert/strict";import { percentDone } from "./score.ts";test("2 of 3 done is 67 percent", () => { assert.equal(percentDone(2, 3), 67);});
node --test prints, among its other lines (the time in brackets is in
ms, milliseconds, and yours will differ):
Outputtext
✖ 2 of 3 done is 67 percent (0.954ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 7 !== 67
Read the report, then fix percentDone below (here without its
export) so the test passes.
Loading the editor…
Exercise · Read the code
Find the line to change
TaskKit writes every due date as text: year, month, day (lesson 14.5).
npm run check refuses this file:
rent.ts:6:54 - error TS2322: Type 'number' is not assignable to type'string'.6 const rent: Task = { title: "Pay rent", minutes: 10, due: 20261201 }; ~~~ rent.ts:4:3 - The expected type comes from property 'due' which isdeclared here on type 'Task' 4 due: string; ~~~Found 1 error in rent.ts:6
The two long lines, the one that starts rent.ts:6:54 and the one
that starts rent.ts:4:3, are one line each in your terminal, broken
here to fit. Which line of rent.ts do you change?
Exercise · Fix the error
Read a count from a web API
A web API for to-do lists answers with JSON (JavaScript Object
Notation, values written as text) like {"open": 3}. JSON.parse
gives back any (lesson 12.1), and openCount takes it as unknown,
the safe box of lesson 8.5, so TypeScript refuses data.open with
'data' is of type 'unknown'. Make openCount give back that number
when data is an object with a number under open, and 0 for
anything else.
Loading the editor…
Exercise · Write the code
Set up a clone and add a package
A friend clones TaskKit to add one thing: ms, a package that turns
text like "2h" into milliseconds. ms brings no types of its own;
they are on npm as @types/ms. Print, one per line, the commands she
types after git clone https://github.com/ana/taskkit. In order: go
into the folder; install exactly the packages the lockfile lists;
add ms, pinned to one version; add its types.
Loading the editor…
Recap
Recap · 3 questions
Check yourself
You reread a cheat sheet and every row looks familiar.
What does that tell you?
Check yourself
Your answer prints the right line for the task's
example. What do you try next?
Check yourself
You cannot start the files and web APIs task without a
lesson open. What now?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Self-assessment: are you ready for Level 2?
What things are
Word
What it means
Self-assessment
A check on yourself: can I do it, with nothing open
Recognising
Knowing a line when you read it
Writing
Making the line yourself, from a blank file
Edge case
An input at the edge of what a task allows
The five skills
Skill
Taught in
Break a problem into steps
Lesson 5.7
Write and test small programs
Lesson 5.11
Read error messages
Lessons 1.5 and 8.1
Work with files and web APIs
Lessons 12.2 to 12.5
Use npm packages safely
Lessons 9.3 and 14.2
Rules
Rule
What it means
Lesson closed
No cheat sheet, no solution in view
A blank file
Write it from the task alone
Edge cases every time
An empty list, a 0, a missing value
The first hint
Nearly yours: the task again tomorrow
The second hint
Needing it counts as a miss
A miss is a lesson
Back to it, then the task again a day later
Module 15 on one page
Cheat sheet
Module 15: Final Project
Finish TaskKit: add a feature that fetches data from an API, and
write a README
Code or rule
What it means
holidaysFrom(data)
A Holiday[], or an error saying what is wrong
Extra features step aside
Save the task first, then ask the API
Tests never call the network
Hand holidaysFrom an answer written out
README
The page that says what a project is and how to run it
[text](address)
A link
Git on your computer: commits and a .gitignore
Code or rule
What it means
.gitignore first
Before the first git add .
git add .
Pick every file .gitignore does not name
git commit -m "…"
Save the picked files as a snapshot
Modified
A saved file that has changed since
Ship TaskKit: publish it on GitHub with a README and a short demo
recording
Code or rule
What it means
gh repo create taskkit --public--source=.--push
Publish the folder
git push
Send your new commits to GitHub
git pull
Bring GitHub's new commits to your computer
Clone
Make your own copy of a repository from GitHub
Self-assessment: are you ready for Level 2?
Rule
What it means
A blank file
Write it from the task alone
Edge cases every time
An empty list, a 0, a missing value
A miss is a lesson
Back to it, then the task again a day later
Where to go next
Level 1 is done. You can plan a program in small steps, write it and
test it, read what TypeScript and Node tell you, work with files and
web APIs, and add packages without trusting them blindly. Now you
also know which of those you can do from a blank file. Passed all
five without a hint: you are ready for Level 2.
Next is Level 2, Practical TypeScript, which is still being
written. It opens with a placement quiz, and its first lesson,
tsc: --noEmit, --watch, and reading its output, is where the
check you have run since Module 1 becomes a tool you drive yourself,
and a new project starts: a typed client for a web API.
Before you move on, write down every task you missed today, with the
lesson behind it. Go back to those lessons first, and tomorrow do the
same tasks again, from a blank file.