A catalogue of common TypeScript errors and how to fix them
Put the common TypeScript errors in one place, like the few dashboard lights most drivers ever see: tell a message’s family from its first words, read a long message down to its deepest indented line, and decide whether the value or the type is the one to change. Break TaskKit once to see a message name both places.
Fundamentals
TypeScript
A catalogue of common TypeScript errors and how to fix them. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Since lesson 1.4, TypeScript has refused your code many times, and
each time the lesson you were reading explained that one message.
Count them across seven modules and you have met 41 different error
codes. A week from now, TS2345 (TS for TypeScript, then the number
of the message) turns up in TaskKit, and all you remember is that you
have seen it before.
A car's dashboard can light up dozens of symbols, but most drivers
only ever see a few: low fuel, a door left open, a seat belt. Each one
has a first thing to check, and nobody needs the whole manual for
them. TypeScript's messages work the same way: a few sentences come up
most of the time. In this lesson you put the common ones in one place,
read a long message down to its deepest line, and decide which line to
change: the value TypeScript points at, or the type it names further
down.
A few sentences, again and again
TypeScript has more than a thousand error messages, but in your code
almost all of them start in one of about a dozen ways. Messages that
start the same way form a family: they mean the same kind of
trouble and are fixed the same way. The error code tells the members
of a family apart. TS2304 and TS2552 are both Cannot find name;
the second one adds a guess, Did you mean …?.
Here are the families you have met: how each message starts, its codes
with the lesson where each first came up, and the question to ask
first. The … stands for the names and types in your own code, and a
number such as the 2 in Expected 2 arguments changes with your
code too.
Cannot find name '…': TS2304 (2.7), with a guess TS2552
(1.4). Is it spelled the same as where the box was made? Was it
made where this line can reach it, not inside another block?
Type '…' is not assignable to type '…': TS2322 (2.2), with a
guess TS2820 (7.2). Which one is wrong, the value or the type?
Argument of type '…' is not assignable to parameter of type '…': TS2345 (1.6). The same, for a value handed to a function.
Property '…' does not exist on type '…': TS2339 (2.4), with a
guess TS2551 (2.4). Is it spelled as in the type? Is the value
narrowed to the shape that has it?
Property '…' is missing in type '…': TS2741 (6.1); for several
properties at once, Type '…' is missing the following properties from type '…', TS2739 (6.4). Add them to the value, or mark
them optional in the type?
'…' is possibly 'undefined': TS18048 (3.3), and TS18047
with 'null' (3.5). Check it before you use it, or give a
default with ??.
Expected 2 arguments, but got 1.: TS2554 (5.1). How many
parameters does the function have?
Cannot assign to '…': TS2588 for a constant (2.1), TS2540
for a read-only property (6.2). Should it change at all? A
constant that must change becomes let, or the new value goes in
a new box. A read-only property stays as it is.
This comparison appears to be unintentional: TS2367 (2.3).
What can each side hold? Often a word spelled two ways.
Object literal may only specify known properties: TS2353
(6.6). Is the property misspelled, or missing from the type?
Block-scoped variable '…' used before its declaration.:
TS2448 (5.2). Is the box made above the line that uses it?
',' expected., Unterminated string literal.: TS1005 and
TS1002 (1.5). Fix the first error first, and if the line it
names looks fine, read the line above it.
Look at the codes in the last family. The 1000s codes you have met so
far are about how the program is written, before any type is looked
at: a missing quote or bracket (1.5), or parameters in the wrong
order, TS1016 and TS1014 (5.3). A missing character can make
TypeScript misread everything after it, so fix these first. The other
codes are about names and types.
Here is a file with five mistakes, from five different families.
Press Run, and before you read on, find each message's family:
Line 8 is family 2: "no" is a string, and done holds a boolean.
Line 11 is family 4, with a guess, and line 12 family 1, without one:
rnet is spelled too differently from rent for TypeScript to guess.
Line 13 is family 6, because at may find nothing, and line 14 is
family 7. Each first question leads straight to the fix: line 8 gets
false, line 11 title, line 12 rent, line 13 a check that first
is not undefined, and line 14 a second number for left.
Reading a long message
Most messages are one line. Some have more lines under the first,
each one indented further. Say you draft a task in a box first, then
push it onto the list. You meant "due on the 5th":
Press Run: the page shows the whole message on one line. Your
terminal gives each part a line of its own. To see it there, save the
code as due.ts in ~/projects/taskkit and check it with tsc
(short for TypeScript compiler), which npx starts:
The first line is broken into three here to fit the page:
Read it one question at a time, as in lesson 1.5. Here the questions
follow the message down its indented lines. Press Next step to
walk through them, or let it play:
Where?
due.ts:7:12: file, line 7, column 12. Under the message, line 7
is copied with its number, and ~~~~~ marks draft, the value
handed to push.
Which code?
TS2345, the number to search for. It starts Argument of type: a value handed to a function.
What does not fit?
The first line: the whole object, { title: string; due: number; }, does not fit Task.
Which part?
One step in, two spaces deeper: the property due is where the
two differ.
Why?
The deepest line: a number where Task wants a string.
This is the line to fix.
How many?
Found 1 error in due.ts:7: one mistake. Fix it, then check
again.
A long message reads from the top down: the deepest indented
line is the reason
All 6 steps shown.Step 1 of 6: Where?Step 2 of 6: Which code?Step 3 of 6: What does not fit?Step 4 of 6: Which part?Step 5 of 6: Why?Step 6 of 6: How many?
All 6 steps shown.
The indentation follows how TypeScript checks. It holds the object
against Task one property at a time, as the figure in lesson 6.1
showed, and reports each step inside on a line of its own, two spaces
further in. The first line can be long: the object's type has no name
of its own, so it is written out in full, as in lesson 7.5.
The deepest line says number, not 5: an object made without a
written type gives each property the general type of its value,
because a property can be changed later (lesson 7.2 showed the same
for text).
due.ts was only for this test: delete it, as in lesson 7.6.
The value or the type?
Every not assignable message is about two things that disagree: a
value, and the type it has to fit. TypeScript knows they disagree.
It cannot know which of the two you meant. Find out where each one
is written in your own project:
1
Break a status in TaskKit
In index.ts in ~/projects/taskkit, find the tasks.push line in
the add case and change its status: "todo" to
status: "to do". You did this in lesson 7.2 with "Todo"; this
time, read the end of the message. Save, and check:
The check stops with TS2820: Type '"to do"' is not assignable to type 'Status'. and the guess Did you mean '"todo"'? from lesson 7.2, on
the push line, where the value is. Below the copy of the push line
and its ~~~, the message prints the extra help, and under that a
copy of the line the help points at:
Its first line is one line in your terminal, broken in three here.
2
Put it back
Change "to do" back to "todo", save, and check again. The check
prints nothing.
The indented part is the extra help you met in lesson 1.5 as
'version' is declared here.: not a second error, but a pointer.
This time it points at the type the value has to fit: line 7 of
index.ts, inside Task, where status is given the type Status.
So the message names both places: the value on the push line,
the type on line 7.
Think of a type as a plan you wrote down, and of a value as one thing
made to fit it. Which of the two to change depends on what you meant,
and only you know that. Here is the due date from the last section,
fixed both ways (no need to make the file again):
Change the value
Does: The value follows the type: a due date is text
Change the type
Does:
The type changes, and every line using due is checked again
The message says where value and type disagree; you
decide which one is wrong
When the type is right, as TaskKit's two statuses are, change the
value. When it was wrong, change it at the line the extra help names,
and fix each line TypeScript then lists.
Predict the line to change
You get this message for a task with four properties, one of them id
(short for identifier, the number that names a task), without seeing
the code. Its first line is broken into four to fit the page. Read it
down to the deepest line.
Predict
Which property's value do you change?
The common mistake: widening the type
When a message will not go away, it is tempting to widen the type,
to change it so that it allows more values, until the message goes.
The message goes, and the mistake stays.
Common mistake
Making the type wider until the message goes away
TypeScript refused "to do". Instead of fixing the word, you let
status hold any text, and the check passes:
Run it: it prints Done: Pay rent, for a task nobody did. As in
lesson 7.2's figure, string lets the typo through.
Done: Pay rent
The fix
Put the type back and change the value, as the guess said:
Now it prints Still to do: Pay rent. Before you change a type, say
in one sentence what it should allow. If the value is not among those
things, the value is the mistake.
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
Why do some lines of a long message start further in?
Check yourself
What do the first words of a message tell you?
Check yourself
A value and its type disagree. What do you change?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
A catalogue of common TypeScript errors
What things are
Word
What it means
Family
Messages that start alike and are fixed alike
Error code
Tells a family's members apart: TS2304, TS2552
Indented line
One step deeper into the value
The deepest line
The reason, and where the fix goes
Expected type comes from
Extra help: the line where the type is written
The catalogue
Code
The message, and what to ask first
TS2304, TS2552
Cannot find name: spelled right, and within reach?
TS2322, TS2820
Not assignable: is the value wrong, or the type?
TS2345
Argument of type: the same, for a value handed to a function
TS2339, TS2551
Does not exist: spelled as in the type? Narrowed?
TS2741, TS2739
Is missing: add it, or mark it optional with ?
TS18048, TS18047
Possibly undefined or null: check, or use ??
TS2554
Expected arguments: how many parameters are there?
TS2588, TS2540
Cannot assign: let for a constant; read-only stays
TS2367
Comparison: what can each side hold?
TS2353
Known properties: misspelled, or missing from the type?
TS2448
Used before its declaration: is the box made above?
Where to go next
You can now tell what kind of error a message is from its first words,
read a long one down to its deepest line, and decide whether the value
or the type is the one to change.
Next is Type errors vs runtime errors: why types vanish when the
code runs. Every message in this lesson came from TypeScript
before a single line ran. That lesson shows the errors that appear
only while the program runs, and why TypeScript cannot catch them.
Before you move on, break TaskKit's index.ts three ways, from
three families of the catalogue: misspell a name, misspell a property,
and give a task a status that is not "todo" or "done". Before
each check, say which family's first words you expect. Then fix
each one and check until the check prints nothing.
TS1005, TS1002
How the code is written: fix the first one first
How to fix
Rule
What it means
Read to the end
The deepest line is the reason
Two places
The value's line, and the type's in the extra help
Right type
Change the value
Wrong type
Change the type, then check every line it lists
Never widen to hide
A wider type lets the mistake through
Exercise · Read the code
Ask the first question
TypeScript refuses line 5 of this file. What do you ask first?
Exercise · Read the code
Where a new status goes
TaskKit should get a third status, "doing". You try
status: "doing" in the add case and check. The message ends
with the lines below. Which line do you change, so that "doing"
is allowed?
Outputtext
due.ts:7:12 - error TS2345: Argument of type'{ title: string; due: number; }' is not assignableto parameter of type 'Task'. Types of property 'due' are incompatible. Type 'number' is not assignable to type 'string'.7 tasks.push(draft); ~~~~~Found 1 error in due.ts:7
The end of the messagetext
index.ts:7:3 - The expected type comes from property 'status' which is declared here on type 'Task' 7 status: Status; ~~~~~~
Outputtext
add.ts:9:12 - error TS2345: Argument of type'{ id: number; title: string; done: string;due: string; }' is not assignable to parameterof type 'Task'. Types of property 'done' are incompatible. Type 'string' is not assignable to type 'boolean'.
This program counts the tasks that are done. Check lists three
errors, each from a different family of the catalogue. Fix all
three, so it prints Done: 1 of 2.
Loading the editor…
Exercise · Fix the error
When the type is wrong
These minutes are meant to be added up: the program should print
Minutes in all: 35. TypeScript refuses lines 5 and 6 with Type 'number' is not assignable to type 'string'. Decide whether the
values or the type are wrong, and fix that one.
Loading the editor…
TypeScript's messagetext
Property 'done' is missing in type'{ title: string; }' but required in type 'Task'.
The end of the messagetext
index.ts:7:3 - The expected type comes from property 'status' which is declared here on type 'Task' 7 status: Status; ~~~~~~