Change a property of an object, mark one a value may leave out with a question mark, and lock one with readonly; see why const keeps the box but not the object inside, and why Node never sees readonly. TaskKit’s tasks get a fixed id and a due date they may leave out.
TypeScript
Fundamentals
Optional and `readonly` properties. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of the last lesson, TaskKit printed #3 Buy milk, due: not set, and every task in it carried due: null, because the
outline said every task must fill that blank. Most of your tasks
will never have a due date. And when you pay the rent, the
{ title: "Pay rent", done: false } task from lesson 6.1 has to say
so: done must change from false to true.
One thing must never change at all: task #2 stays #2, or TaskKit
loses track of which task is which.
Think of a paper form again. Some blanks say "Middle name (if
any)", and you may leave them empty. Some are printed by the
office, like the form's number, and you never write over them. The
rest you fill in, and cross out and fill in again when something
changes. In this lesson you write all three into an object type.
By the end you will change a property, mark the due date as one a
task may leave out, and mark the id (short for identifier, the
number that names a task) as one nobody may change. TaskKit's tasks
will lose their due: null.
Changing a property
You read a property with a dot. You change one the same way you
refill a let box: the property, =, and the new value. Press
Step to watch line 3 do it:
It prints false, then true, then the whole task with
done: true in it. The other two properties are untouched: =
after a property changes that property and nothing else.
task was made with const, and line 3 still changed it. Lesson
4.1 found the same thing with a list: const locks the box, so it
will hold this same task for as long as the program runs, but the
task inside can still change. Only a line that puts a whole new
object in the box, task = { … } (the dots stand for any task), is
refused, with TS2588 (TS for TypeScript, then the number of this
kind of mistake).
The outline is checked on every change, too. A property keeps the
type it was made with, so done takes only true or false:
TypeScript refuses line 2 with Type 'string' is not assignable to type 'boolean'. A property the outline does not have cannot be
added later either. task.due = "Friday" is refused with
TS2339, Property 'due' does not exist on type, followed by the
task's type. If a task may get a due date, its outline has to say
so from the start. The next section shows how, without making
every task write one.
Optional properties: a blank you may skip
A task with no due date still writes due: null, "empty on
purpose". For a list where most tasks never get a date, that is a
line of nothing in every one of them.
An optional property is one a value may leave out. You mark it
with a question mark after its name, the same mark that made a
parameter optional in lesson 5.3:
due?: string reads "a due date may be here; if it is, it is
text". This task has none, and TypeScript accepts it. title and
done have no ?, so they are still required. A task that left
due out can still get one later: task.due = "Friday" is accepted
now, because the outline has a place for it.
Here is the outline checking three values:
{ title: "Pay rent", done: false, due: "Friday" },
{ title: "Call Mom", done: false } and { title: "Buy milk" }.
Switch between them to see where each one stops, 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?
done: boolean: passes✓Is done there, and true or false?
due?: string: passes✓Left out, or there and text?
Node: reachedRuns every line
A property marked with ? may be left out; the others may not
All 8 steps shown.Step 1 of 8: The valueStep 2 of 8: title: stringStep 3 of 8: title: string: passesStep 4 of 8: done: booleanStep 5 of 8: done: boolean: passesStep 6 of 8: due?: stringStep 7 of 8: due?: string: passesStep 8 of 8: Node
All 8 steps shown.
The value: reachedThe object you wrote
title: string: passes✓Is title there, and text?
done: boolean: passes✓Is done there, and true or false?
due?: string: passes✓Left out, or there and text?
Node: reachedRuns every line
A property marked with ? may be left out; the others may not
All 8 steps shown.Step 1 of 8: The valueStep 2 of 8: title: stringStep 3 of 8: title: string: passesStep 4 of 8: done: booleanStep 5 of 8: done: boolean: passesStep 6 of 8: due?: stringStep 7 of 8: due?: string: passesStep 8 of 8: Node
All 8 steps shown.
The value: reachedThe object you wrote
title: string: passes✓Is title there, and text?
done: boolean: stops here✕Is done there, and true or false?
due?: string: never reachedLeft out, or there and text?
Node: never reachedRuns every line
done: boolean stops hereProperty 'done' is missing in type '{ title: string; }'
but required in type '{ title: string; done: boolean;
due?: string | undefined; }'.
A property marked with ? may be left out; the others may not
All 5 steps shown.Step 1 of 5: The valueStep 2 of 5: title: stringStep 3 of 5: title: string: passesStep 4 of 5: done: booleanStep 5 of 5: done: boolean: stops here. Property 'done' is missing in type '{ title: string; }'
but required in type '{ title: string; done: boolean;
due?: string | undefined; }'.
All 5 steps shown.
A task with a due date passes, and so does one without. Leaving out
done still stops the check, because nothing marked it optional.
Run a list that holds both kinds of task:
It prints Pay rent, due: Friday and Call Mom, due: not set.
Look at how the due date is read. Lesson 6.1 showed that reading a
property an object does not have gives undefined. So TypeScript
gives task.due the type string | undefined, as it did for an
optional parameter in lesson 5.3.
Before you use it as text, give it a default with ??, as in
lesson 3.4, or check task.due !== undefined first. Write
task.due.toUpperCase() without either, and TypeScript stops you
with the message from lesson 3.3:
'task.due' is possibly 'undefined'.
Read-only properties: a blank filled once
A task's id is its name for good. TaskKit prints it in front of
every title, and you will use it to point at one task. If some line
quietly turned #2 into #7, the number you remember would point at
the wrong task, or at none.
A read-only property can be read like any other, but never
changed after the object is made. You mark it with the word
readonly before its name in the type:
Line 8 changes the title, which is allowed. Line 9 is refused:
Cannot assign to 'id' because it is a read-only property. The
code is TS2540, and its first words, Cannot assign to, are the
same as for a const box in lesson 2.1. That is where the two are
easy to mix up. const locks a box. readonly locks one property,
whatever box the object is in. Here is a task typed
{ readonly id: number; title: string; done: boolean }, in a
const box and in a let box:
The lineconst tasklet tasktask = { … }
Refused:TS2588
The box keeps its task.
Allowed
Another task goes in.
task.done = true
Allowed
The task itself changes.
Allowed
The task itself changes.
task.id = 9
Refused:TS2540
id is read-only.
Refused:TS2540
id is read-only.
The box
const decides the box; readonly decides the property, in any box
Read it one line at a time: the box decides only the first line;
the type decides id in both boxes.
readonly belongs to TypeScript, not to JavaScript, so only the
check knows about it. Here is one file, run by Node and then
checked by TypeScript:
What node task.ts prints. readonly is not JavaScript: Node never
sees it, so the id quietly becomes 9.
task.ts:8:6 - error TS2540:
Cannot assign to 'id' because
it is a read-only property.
TypeScript checks it
Does: It names the property and refuses the line
What npx tsc --noEmit--types node task.ts prints, first line
(tsc is short for TypeScript compiler, which npx starts).
Check before you run, and the line never reaches Node.
The same line: Node changes the id, TypeScript refuses it
So readonly is a promise TypeScript keeps for you while it
checks. Give it to the properties that must never change, like an
id, and not to everything: if done were read-only, no task could
ever be finished.
TaskKit: a fixed id, an optional due date
Now TaskKit's tasks get both marks. Their id never changes after
they are added, and their due date may be left out, so due: null
goes.
1
Mark the id and the due date
In index.ts in ~/projects/taskkit, the outline of a task is
written twice: on the list, and on printList's parameter. Change
both. The list becomes:
============================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
npx tsc prints nothing: the check passed. node --test still
counts pass 7.
The output is exactly what it was, and that is the point: what
changed is what TypeScript now checks. Every task may now leave its
due date out, and no line in TaskKit can renumber a task without
TypeScript stopping it.
Predict before you run
Three tasks, two with a due date. One line changes the first task,
then the loop prints the tasks that are not done. Decide what it
prints before you run it.
You made due optional in TaskKit, as in the steps above, but one
line written for the old outline still adds a task with
due: null. It looks harmless: null meant "no due date" a minute
ago.
Common mistake
Writing null into an optional property
The outline has due?: string, and the new task says null:
The message says what ? really means: the property is a string
or undefined. null is neither, and lesson 3.3 showed that
null === undefined is false. When you see string | undefined
in a message about a property you never wrote undefined for, look
for a ? in its type. If you do want "empty on purpose" written
into every value, keep due: string | null instead.
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.
Exercise · Read the code
Which task may leave a blank
A list holds tasks of type { title: string; due?: string }.
Which of these can you add to it?
Exercise · Fix the error
Let a task leave its due date out
TypeScript refuses this task with Property 'due' is missing.
Calling Mom has no due date. Change the type, not the task, so the
program prints Call Mom, due: not set.
Loading the editor…
Exercise · Fix the error
Unlock what has to change
This type locks too much: you must be able to mark a task done,
but never change its id. TypeScript refuses line 6. Fix the type so
the program prints #1 Pay rent, done: true, and keep the id
read-only.
Loading the editor…
Exercise · Write the code
Write the due date line
Write the function dueLine, which takes a task and gives back its
title with its due date in brackets, or the title alone when it has
none: for { title: "Pay rent", due: "Friday" } it gives back
"Pay rent (due Friday)", and for { title: "Call Mom" }, just
"Call Mom".
Loading the editor…
Recap
Recap · 3 questions
Check yourself
What does due?: string say about a task?
Check yourself
A const task, then task.done = true. What happens?
Check yourself
Node runs task.id = 9 on a readonly id. What happens?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Optional and readonly properties
What things are
Word
What it means
Optional property
One a value may leave out, marked with ?
Read-only property
One nobody may change, marked readonly
const box
Keeps its object; the object can still change
Code
Write
To
task.done = true
Change one property
due?: string
Let a value leave due out
task.due ?? "not set"
Read an optional one, with a default
readonly id: number
Make a property read-only
Rules
Rule
What it means
Same type on change
task.done = "yes": TS2322
Only what the outline has
A new property later: TS2339
Optional reads
string | undefined; check before use
No null for ?
due?: string refuses null: TS2322
Read-only
Any change refused: TS2540
Node alone
Never sees readonly, changes it anyway
Where to go next
You can now change a property with =, let a value leave a
property out with ?, and lock a property with readonly. You
also know which lock is which: const keeps the box, readonly
keeps the property. TaskKit's tasks have a fixed id and a due date
they may leave out.
Next is Type aliases (type). TaskKit now writes
{ readonly id: number; title: string; due?: string } in two
places, and every new function that takes a task would write it
again. There you give the outline a name, Task, and write it
once.
Before you move on, in any code block on this page, make a task
like the one from the end of the last lesson, give it a type with
an optional note and a read-only id, and change its done to
true. Then try to change the id and read the whole message, down
to read-only property.