Keep the facts about one task in one value: write an object, read its properties with a dot, and write the object type TypeScript holds every such value against, for a box, a parameter or a whole list. TaskKit’s tasks become objects with an id, a title and a due date.
Fundamentals
TypeScript
Objects and object types. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of Module 5, TaskKit's functions got their tests, and
every task in it is still one string: "Pay rent". Now you want to
know more about each task. How long will it take? Is it done yet?
So you keep three lists side by side, the titles in one, the
minutes in another, done or not in a third, and they work, until
you sort the titles from A to Z. The titles move. The minutes stay
where they were, and your program tells you that buying milk takes
fifteen minutes.
Your phone never makes that mistake with your contacts. It does
not keep every name in one list and every number in another; each
person is one card, with labelled lines on it: name, number,
birthday. Move the card and everything on it moves too. In this
lesson you make that card in code. By the end, a task will be one
value holding its title, its minutes and whether it is done,
TypeScript will check every part of it, and TaskKit's tasks will
carry an id (short for identifier, a number that names one task),
a title and a due date.
One task, three facts
Here is that version with two of the lists, the titles and the
minutes. The titles are sorted with toSorted and localeCompare
from lesson 4.4, and the loop prints each title with the minutes at
the same index (min is short for minutes). Run it:
Nothing is wrong with any single line, and TypeScript has nothing
to complain about. The bug is in the shape. The two lists belong
together only because you remember that index 0 in one is index 0
in the other, and toSorted knows nothing about that: it made a
sorted copy of the titles and left the minutes as they were.
buy milk: 15 min
call Mom: 10 min
pay rent: 5 min
Two lists, titles sorted
Never: The minutes stay where they were
What node lists.ts prints. Buying milk takes 5 minutes, not 15:
only the titles moved.
buy milk: 5 min
call Mom: 10 min
pay rent: 15 min
One object per task, sorted
Does: Each title moves with its own minutes
What node tasks.ts prints. Every fact about a task is in one
value.
Sorted as two lists, facts drift apart; as objects, they stay
An object: one value, named parts
An object is one value made of named parts. You write it
between curly braces, { and }, as a list of parts separated by
commas, and each part is a name, a colon and a value:
That is one value, in one box called task, and it holds three
facts: a string, a number and a boolean. In lesson 2.4 you met the
word property: a fact a value carries, like a string's
.length. That is exactly what each part of an object is. title,
minutes and done are its properties, and "Pay rent", 15 and
false are their values.
You read a property with the same dot you used for .length: the
object's name, a dot, the property's name. Run this:
task.title is the string "Pay rent", and task.minutes is a
number you can count with: 15 + 5 prints 20. The last line
prints the whole object, every property with its value. Node writes
the text in single quotes, 'Pay rent'; it is the same string you
wrote with double ones.
An object can go anywhere a value can, including into a list. Here
are the three tasks from the start, one object each, sorted by
their titles. a and b are now whole tasks, so the rule compares
a.title with b.title:
Buying milk is back to five minutes. toSorted moves whole
objects, and each one carries its own minutes with it.
Object types: an outline the value must fit
TypeScript works out a type for every box you fill, as you saw in
lesson 2.6, and an object is no exception. For the task above it
works out this, and writes it this way in its messages:
That is an object type: the outline every such object must fit,
with the same names as the object, each with the kind of value it
must hold instead of the value itself. Think of a paper form with
three labelled blanks: the type is the empty form, and an object is
the form filled in. TypeScript accepts a value only when every
blank on the form is filled, and filled with the right kind of
thing.
When you write the type yourself, it goes after the box's name and
a colon, the way : string did in lesson 2.2. Two marks tell the
type from the value: the type separates its parts with semicolons,
the value with commas. TypeScript accepts commas in a type too, but
its own messages, and most code you will read, use semicolons.
TypeScript's messages also put one after the last part; you may
leave it out, as below.
Here the type changes nothing, because TypeScript would have
worked out the same one. It earns its place when the value is not
written right beside it: a list that starts empty, or a function
waiting for a task someone else will pass in. Then the type is the
only place that says what a task must look like.
TypeScript holds the value against the type one property at a
time. Switch between the three values to see where the two broken
ones stop, and press Next step to walk through it, or let it
play:
The value: reachedThe object you wrote
title: string: passes✓Is title there, and text?
minutes: number: passes✓Is minutes there, and a number?
done: boolean: passes✓Is done there, and true or false?
Node: reachedRuns every line
The value is held against its outline, one property at a time
All 8 steps shown.Step 1 of 8: The valueStep 2 of 8: title: stringStep 3 of 8: title: string: passesStep 4 of 8: minutes: numberStep 5 of 8: minutes: number: passesStep 6 of 8: done: booleanStep 7 of 8: done: boolean: passesStep 8 of 8: Node
All 8 steps shown.
The value: reachedThe object you wrote
title: string: passes✓Is title there, and text?
minutes: number: stops here✕Is minutes there, and a number?
done: boolean: never reachedIs done there, and true or false?
Node: never reachedRuns every line
minutes: number stops hereType 'string' is not assignable to type 'number'.
The value is held against its outline, one property at a time
All 5 steps shown.Step 1 of 5: The valueStep 2 of 5: title: stringStep 3 of 5: title: string: passesStep 4 of 5: minutes: numberStep 5 of 5: minutes: number: stops here. Type 'string' is not assignable to type 'number'.
All 5 steps shown.
The value: reachedThe object you wrote
title: string: passes✓Is title there, and text?
minutes: number: passes✓Is minutes there, and a number?
done: boolean: stops here✕Is done there, and true or false?
Node: never reachedRuns every line
done: boolean stops hereProperty 'done' is missing in type
'{ title: string; minutes: number; }' but required in type
'{ title: string; minutes: number; done: boolean; }'.
The value is held against its outline, one property at a time
All 7 steps shown.Step 1 of 7: The valueStep 2 of 7: title: stringStep 3 of 7: title: string: passesStep 4 of 7: minutes: numberStep 5 of 7: minutes: number: passesStep 6 of 7: done: booleanStep 7 of 7: done: boolean: stops here. Property 'done' is missing in type
'{ title: string; minutes: number; }' but required in type
'{ title: string; minutes: number; done: boolean; }'.
All 7 steps shown.
Run the two that stop. In the first, the minutes were typed as
text, "15", the way process.argv (the argument vector, Node's
list of the words you typed, from lesson 2.8) hands them over:
The message, TS2322 (TS for TypeScript, then the number of this
kind of mistake), points at the property and says what it found and
what the type wants: a string where a number belongs. In the
second, done was forgotten:
This message, TS2741, is longer, and it reads in three parts:
Property 'done' is missing, then the type of the value you wrote,
which has no done, then but required in type, followed by the
type it had to fit. When a message quotes two object types, look
for the property one has and the other does not.
Objects in functions and lists
A function that works on a task can take the whole task as one
parameter. Its type is an object type, written where : number
was written in lesson 5.1:
Inside the function, task.title is a string and task.minutes
a number, because the parameter's type says so. Outside it, every
call is checked against that outline. Add a call that forgets the
minutes, describe({ title: "Call Mom" }), and TypeScript refuses
it with the message you met above: Property 'minutes' is missing.
Without that check, Node would run it and print
Call Mom: undefined min.
A list of objects has a type too: the object type followed by
[], the same [] that made string[] in lesson 4.1. You need it
for a list that starts empty, because TypeScript cannot work out
what will go into []:
From then on, every push is held against that outline.
today.push({ title: "Call Mom", minutes: "10", done: true }) is
refused at minutes with Type 'string' is not assignable to type 'number'.
You may have noticed that you are writing the same outline over and
over. Lesson 6.3 gives it a name, so you write it once.
TaskKit: tasks become objects
Now TaskKit's step for this module: each task becomes an object
with three properties. id is the number that names the task
and never changes, given when it is added: 1 for the first, 2 for
the second.
title is the text you typed. due is the due date. Nobody
can type one yet, so every task starts with null, "empty on
purpose", from lesson 3.3, and its type is string | null.
1
Give the list an object type
In index.ts in ~/projects/taskkit, find
const tasks: string[] = []; and replace it with this line, the
outline of a task in place of string:
2
Add objects, not strings
In the add case, two places change. sameTask compares two
strings, so hand find the titles rather than the tasks: map
from lesson 4.3 makes a list of them. Replace the line that makes
twin with the two lit lines:
Then push a whole task where you pushed the string. The list's
length before the push, plus one, is the new task's id:
3
Print the id and the due date
Replace printList with this version. It takes a list of tasks,
sorts them by title, and prints each one's id, title and due date;
?? from lesson 3.4 prints not set for a null:
4
Check, run TaskKit, run the tests
Check the file with tsc (short for TypeScript compiler), which
npx starts, run TaskKit, then run the tests:
npx tsc prints nothing: the file passes. Then TaskKit prints:
node --test still counts pass 7: the functions it tests did not
change.
Look at the ids. Buy milk was added third, so it is #3, and
sorting moved it to the top of the list with its id still on it.
Predict before you run
Three tasks, one of them done. Read the code and decide what it
prints before you run it.
Predict
What is the second line this program prints?
The common mistake: a misspelt property
You type task.titel instead of task.title. In plain JavaScript
that is not an error at all: asking an object for a property it
does not have gives back undefined, the "nothing" from lesson
3.3, and the program carries on. Node runs your .ts files without
checking their types, so it does exactly that. TypeScript does
check:
Next up: undefined
Node runs it
Never: It never says titel is not a property
What node task.ts prints. A part that is not there reads as
undefined, and the program carries on.
task.ts:2:30 - error TS2551:
Property 'titel' does not exist
on type '{ title: string;
minutes: number; done: boolean;
}'. Did you mean 'title'?
TypeScript checks it
Does: It names the part and suggests the right one
What npx tsc --noEmit--types node task.ts prints first, broken
here to fit the card; in your terminal it is one line. TS2551 is
the number of this message.
The same typo: Node runs it quietly, TypeScript stops it
Common mistake
Reading a property under the wrong name
The object has a title, and the line asks for a titel:
Property 'titel' does not exist on type '{ title: string; minutes: number; done: boolean; }'. Did you mean 'title'?
The fix
Use the name the object was made with, the one the message
suggests:
The message quotes the whole type, so you can read off every name
the object really has, and Did you mean usually names the one you
wanted. If an undefined turns up in your output where a value
should be, check the property's spelling before anything else; in
VS Code (Visual Studio Code, your editor), the red line under it
was already telling you.
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 is an object?
Check yourself
The type needs minutes, the task has none. What happens?
Check yourself
Node runs a file that reads task.titel. What does it get?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Objects and object types
What things are
Word
What it means
Object
One value made of named parts
Property
One named part, like title: "Pay rent"
Object type
The outline a value must fit
The dot
Reads one property: task.title
Code
Write
To
{ title: "Pay rent" }
Make an object; commas between parts
task.minutes
Read one property
{ title: string }
Write an object type; semicolons between
task: { title: string }
Give a box or a parameter that type
{ title: string }[]
Type a list of such objects
a.title.localeCompare(b.title)
Compare by title, for toSorted
Rules
Rule
What it means
Every property there
A missing one: TS2741, is missing
Each of the right kind
A wrong one: TS2322, not assignable
Names spelt right
TS2551: Did you mean 'title'?
Node alone
A misspelt property reads undefined
Where to go next
You can now keep the facts about one thing in one object, read
each of them with a dot, and write the outline TypeScript holds
every such value against, for a box, a parameter or a whole list.
TaskKit's tasks are objects now, with an id that stays with each
task however the list is sorted.
Next is Optional and readonly properties. Every task so far
had to fill every blank on the form, so a task with no due date
carried a null. There you mark a property as one a value may
leave out, mark another as one nobody may change, and find out how
to change a property at all.
Before you move on, make an object for a task of your own in any
code block on this page, with a title, its minutes and whether it
is done, and print one sentence about it with a template string.
Then misspell one property on purpose, run it, and read the whole
message, down to Did you mean.
Exercise · Read the code
Which value fits the outline
A function wants a task of type
{ title: string; minutes: number }. Which of these can you pass
it?
============================TaskKit 0.1Your tasks, in your terminal============================Added: Pay rentAdded: Call MomAdded: Buy milkTasks added: 3 of 5, room for 2 moreList 60% fullYour list, A to Z:#3 Buy milk, due: not set#2 Call Mom, due: not set#1 Pay rent, due: not set
TypeScript refuses this task with
Property 'done' is missing. Buying milk is not done yet. Fix the
object so it fits its type and the program prints
Buy milk: 5 min.
Loading the editor…
Exercise · Fix the error
Ask for the right name
This program should print Next up: Call Mom. TypeScript refuses
it. Fix it.
Loading the editor…
Exercise · Write the code
Add up the minutes
Write the function totalMinutes, which takes a list of tasks and
gives back all their minutes added together: for paying the rent
(15 minutes) and buying milk (5) it gives back 20, and for an
empty list 0.