Tell a string, a number and a boolean apart, even when they print the same, and write the type on a box with a colon. See why a let box takes a new value but never a new type, read TS2322, and why a number in quotes prints 011 when nothing checks it.
Basic types: `string`, `number`, `boolean`. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of the last lesson TaskKit printed Tasks added: 2.
Look at the boxes that made it: version holds "0.1",
firstTask holds "Buy milk", and added holds 2. Two of them
hold text and one holds a number, and so far you have never had
to say which is which.
Think of a form at the post office. The box for your name takes
letters. The box for the number of parcels takes digits. The box
"Signature required?" takes a tick in Yes or No. Write
"twelve" in the parcels box and the clerk hands the form back
before a single parcel is sent. TypeScript keeps the same kind of
form for every box in your program. By the end of this lesson you
will know the three kinds of value every program starts with, how
to write the kind on a box, and what TypeScript says when a box
gets the wrong one. TaskKit's counter will say it holds a
number.
Every value has a type
Run these two lines:
ts
console.log("2");console.log(2);
Both print 2, and on the screen you cannot tell them apart. In
the program they are different things. The first is text: the
character 2, like the number painted on a door. The second is a
number, one you can count with, add to, and compare with another.
A program has to know which one it holds, because it treats them
differently: a count goes up by one; a door number never does.
You met the word in lesson 1.6: the kind of a value is its type.
Every value in your program has a type, and every box holds values
of one type. TypeScript knows the type of each of them before
anything runs, and that is what lets it check your code: a value of
one type cannot quietly end up where a value of another type
belongs.
Three basic types
Every value you have written so far is text or a number. Those two,
with a third, are the basic types: the three every program
starts with, and the ones the next lessons build on.
A string is text. You met the word in Module 1: a string of
characters, written between double quotes. "Buy milk" is a
string, so is "0.1", and so is "", the empty string, with no
characters at all.
A number is a count or an amount, written with no quotes.
Whole numbers and decimals are the same type: 2, 2.5 and
-1 are all numbers.
A boolean is the answer to a yes-or-no question: is the
task done, is it urgent? It has exactly two values, true and
false, written in lower case and with no quotes.
TypeScript also accepts single quotes for a string, 'Buy milk';
this course writes double quotes, and TypeScript's own messages put
names and types in single quotes, as in 'string'.
Here they are side by side, each in the kind of code TaskKit will
hold:
let added = 2;const price = 2.5;const change = -1;
number
Does: Holds whole numbers and decimals alike
Never: It is never written in quotes
index.ts
const done = false;const urgent = true;
boolean
Does: Holds exactly two values: true and false
Never: No quotes, and no capital letter
Every value is one of three kinds, and its type says which
Look at version. It is made of digits and a dot, and it is still
a string, because it is in quotes. The quotes decide the type, not
what is between them. That one rule is behind the most common
mistake in this lesson, and you will meet it at the end.
The word boolean comes from George Boole, a mathematician who
worked out how to calculate with true and false. In the next
lesson you will get booleans out of questions such as "is 3
bigger than 2?"; for now you write them yourself.
Write the type on the box
Writing the type down tells TypeScript, and anyone who reads the
line, what the box is for, so a wrong first value is caught on that
line. You write it on the line that makes the box: put a colon after
the name, then the type:
ts
let added: number = 0;
Read it as "make a box called added, for numbers, and put 0 in
it". The : number part is a type annotation: a note saying
which type the box holds. It is read by TypeScript and by people;
Node skips it when it runs the file, so it changes nothing about
what the program does.
The three basic types are written the same way, always in lower
case:
Every box now carries its type as well as its name and its value.
Here are boxes like TaskKit's at the end of the last lesson, with
their types; added has just been refilled from 1 to 2:
Every box holds one type of value, and its tag says which
You do not have to write the type on every box. When you leave it
out of a let box, TypeScript takes it from the first value: let added = 0; makes a box for numbers just the same. Rest the pointer
on its name in VS Code (Visual Studio Code, your editor), and it
shows you what TypeScript decided, let added: number. A const
box shows something different there; why, and when to write a type
yourself, is a lesson of its own later in this module.
A box keeps its type
In the last lesson you learned that let lets a later line refill
a box. It lets you change the value. It does not let you change
the type. A box made for numbers takes a new number as often as
you like, and never anything else.
Bad — the refill puts text into a
box that was made for a number.
Run the first one. TypeScript refuses line 3 with TS2322, Type 'string' is not assignable to type 'number'. As in lesson 1.6, not
assignable means "does not fit": a string may not go into a box for
numbers. The box got its type from its first value, 0, and keeps
it for the rest of the program. Every message with TS2322 reads
the same way: the first type is what you tried to put in, the second
is what the box holds.
The same holds for every type. A string box takes any text and
no number; a boolean box takes true or false and nothing
else. One box, one type, from the line that makes it to the end.
TaskKit's counter says it holds a number
Open index.ts in ~/projects/taskkit. Of all its boxes, added
is the counter, the one box whose value you count with, so it is the
one where a wrong first value would do harm. Write its type on the
line that makes it:
index.tsts
let added: number = 0;
Leave every other line as it is. Check the file with tsc (short
for TypeScript compiler), which npx starts, then run it:
bash
npx tsc --noEmit index.tsnode index.ts
Outputtext
============================TaskKit 0.1Your tasks, in your terminal============================Added: Buy milkAdded: Call the dentistTasks added: 2
The check prints nothing and the output has not changed, which is
right: an annotation is for TypeScript, not for Node. What changed
is the line itself. Anyone reading it now sees that added is a
count without reading further, and TypeScript checks the first
value against number too. The common mistake below shows what
that catches.
Every other box in TaskKit holds a value in quotes, "0.1",
"Buy milk", so you start the counter the same way: "0". The box
is meant for a number, but the value is a string.
Common mistake
Writing a number in quotes
The file:
index.tsts
let added: number = "0";added = added + 1;added = added + 1;console.log("Tasks added:", added);
npx tsc --noEmit index.ts refuses line 1: "0" is a string, and
the box is for numbers.
Type 'string' is not assignable to type 'number'.
The fix
Take the quotes off. A number is written as it is:
index.tsts
let added: number = 0;added = added + 1;added = added + 1;console.log("Tasks added:", added);
What happens next depends on whether anything checks the file
first. Press Next step to follow it, or let it play, and switch
between the two ways of running it:
Line 1 makes let added: number and puts "0" in it: a
string, in a box made for a number.
You ran node index.ts straight away, so nothing checked
the file.
Node never looks at types. With text, + 1 glues a 1 on:
"01", then "011".
No error at all: the program ends normally, with a wrong
count.
A number in quotes: caught before running, or printed wrong
All 4 steps shown.Step 1 of 4: Your code: index.tsStep 2 of 4: TypeScript: never askedStep 3 of 4: Node: lines 1–3Step 4 of 4: Screen: Tasks added: 011
All 4 steps shown.
Compare it with the last lesson's mistake. There, Node stopped with
a TypeError halfway through. Here it runs to the end (+ between
a string and a number joins them as text, as the next lesson shows)
and prints a believable wrong count, which is worse than a crash.
Leave the type off as well, and not even TypeScript can help: the
box takes its type from "0" and becomes a box for text, so the
check finds nothing wrong. Press Step to watch the box fill
with text instead of counting:
When you see TS2322, read the two types in the message and look
at the value on the line it names. Quotes around digits, "0",
make a string, and quotes do the same to a boolean: "false" is
a string. Take the quotes off, check again, and the message goes.
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. Two ask you to read code,
one to fix it and one to write it; try the last one in a new file,
types.ts, in your taskkit folder, and check and run it as you
did index.ts.
Exercise · Predict the result
Name the type
Which type does TypeScript give the box price? Answer with one
word.
ts
let price = "2.50";
Exercise · Read the code
Find the boolean
Which line puts a boolean into the box done?
Exercise · Fix the error
Make the values match their boxes
This program should print Buy milk 3 false, but TypeScript
refuses two of its lines. Fix them so the check passes, without
changing any type.
Loading the editor…
Exercise · Write the code
Add a task with its types
Write a program that makes three boxes, each with its type
written on it:
task, a string holding "Water the plants";
added, a number that starts at 0 and is then refilled with
one more than it holds;
done, a boolean holding false.
The checks look at the values only, so writing the types is up to
you. Then print these three lines:
text
Added: Water the plantsTasks added: 1Done: false
Loading the editor…
Recap
Recap · 3 questions
Check yourself
What is the type of a value?
Check yourself
After let added = 0, what can go in the box later?
Check yourself
What does TS2322 tell you?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Basic types: string, number, boolean
What things are
Word
What it means
Type
The kind of a value: text, a number, true or false
string
Text between quotes, even if it is all digits
number
A count or an amount, whole or decimal, no quotes
boolean
true or false, and nothing else
Type annotation
: number after a name: the box's type
Not assignable
Does not fit: not allowed into the box with =
Code
Write
To
let added: number = 0;
Make a box for numbers
let done = false;
Make a box for booleans; its type comes from false
added = 2;
Refill a number box with a number
Rules
Rule
What it means
Quotes make text
"2" is a string; it prints like 2
Lower case
string, number, boolean, true, false
One box, one type
let changes the value, never the type
TS2322
A value of the wrong type went into a box
Check first
Run alone, a number in quotes gives a wrong count
Where to go next
You can now tell a string, a number and a boolean apart in
code, even when they print the same, write a type on a box, and
read TS2322 as "this value does not fit this box". TaskKit's
counter says it holds a number.
Next is Arithmetic, comparisons, and template strings. You
know what kinds of value there are; the next lesson is about what
you can do with them: count with numbers, join strings, and ask
questions whose answers are booleans.
Before you move on, make the common mistake on purpose. In
index.ts, change 0 to "0" on the line that makes added,
check the file and read the message, then run it with node alone
and see what it prints. Take the quotes off, and check once more
before you call it done.