Write the rules from lesson 4.3 the long way: an arrow function with typed parameters and a return type, a short body that gives its value back by itself or a body in braces with return, kept in a box and called by its name. See map call an arrow once per item, learn why a rule inside map needs no types, and when to write an arrow rather than a function declaration.
Fundamentals
TypeScript
Arrow functions. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Pancakes again. Your grandmother's card is in the drawer, with its
name at the top and its steps below, and you follow it every
Sunday. But this Sunday a friend stands at the stove beside you,
and you say: "As each one comes off the pan, sprinkle it with
sugar." That instruction has no name and no card. You say it once,
right where it is needed, and your friend follows it for every
pancake.
Your code already has both kinds. Lesson 5.1 gave you the card:
function left(used: number): number, with a name, at the top of
the file. Lesson 4.3 gave you the instruction said at the stove:
(task) => task.toUpperCase(), handed to map and followed for
every item. They look like two different things, and they are one
thing: both are functions. In this lesson you write arrows the way
you wrote left, with types, with several lines and return, and
in a box of their own. By the end you will know which of the two to
write, and why.
The rules from lesson 4.3 are functions
Here is left from the last lesson, written two ways. The second is
an arrow function, like the rules you handed to map in lesson
4.3, with two things added: the types, and a box that holds it. Lesson
4.3 showed you the short one; now you see the whole of it:
A function declaration
Does: Has a name of its own, after the word function
Never: It never gives a value back without return
An arrow function
Does: Gives back the value after the arrow, with no return
Never: It has no name of its own: a box holds it
One function, written two ways
Read the arrow from left to right. const left = makes a box, as
every const does, and the function goes in it. An arrow has no
name of its own, so the box's name is how you call it.
(used: number) is the parameter, written exactly as in a
declaration. : number after the parentheses is the return type.
Then comes the arrow, =>, and after it the body.
An arrow whose body is a single value, with no braces, has a short
body. That value is what the call gives back, and no return is
needed: the body is that one value, so there is nothing else to give
back. 60 - used is worked out and handed back. That is why the rule
(task) => task.toUpperCase() never needed a return either. Run it:
It prints 35 and then 10, as the declaration did. You call it the
way you called left in lesson 5.1: the box's name, then the
arguments in parentheses, and the same trip into the body and back.
An arrow in a box needs its types
In lesson 4.3 you never wrote a type for task, and lesson 5.1
says a parameter's type is always written. Both are right. What
decides it is where the arrow stands. Here is the same rule handed
to map, kept in a box, and kept in a box with its type:
Handed to map: the list says what comes in
Does: map passes one item of tasks at a time
Kept in a box: nothing says what comes in
Parameter 'task' implicitly has an 'any' type.
Kept in a box, with its type written
Does: Every call of shout is checked against string
Inside map the list gives task its type; in a box, you do
Inside map, the arrow is an argument, and map already says what
it will pass: one item of tasks, which is a string. TypeScript
gives task that type, as the hover showed you in lesson 4.3. In a
box, the arrow stands alone. Its calls come later, from anywhere in
the file, so nothing says what task will hold. That is lesson
5.1's case, with lesson 5.1's error, TS7006 (TS for TypeScript,
then the number of this kind of error), and the same fix: write the
type yourself. Write the return type too, as lesson 5.1 did: it
is the promise TypeScript holds the body to.
Two more shapes you will meet. An arrow that takes nothing has
empty parentheses, and a short body may start on the next line
when the first one is full:
It prints a divider of 28 equals signs and
35 minutes left in the hour. In other people's code you will also
see an arrow with one parameter and no parentheses:
task => task.trim(). It is the same arrow. This course keeps the
parentheses, because a type has to go inside them.
A body in braces
A short body is one value. When the work takes more than one step,
each on its own line, the body goes in braces, as a declaration's
does, and then it needs return. Braces can hold any number of
lines, and the arrow does not guess which one is the answer: without
return, a call gives back undefined. Call it a body in braces.
Here map gets an arrow with a body in braces: lesson 5.1's
capitalize, written for a whole list. Press Next step to
follow it one step at a time, or Play:
You never wrote a call of the arrow: map made the calls, once for
every item, the way the conveyor belt in lesson 4.3 sent every item
past the station, and each return handed its answer to map, not
to your line.
Now press Step on the same code and watch the Call stack
column. While the computer is inside the arrow it says
global › anonymous. An arrow handed to map has no name and no
box, so the player calls it anonymous, which means "without a
name". You have seen the word before, in the last lines of Node's
error messages. An arrow kept in a box is named after its box, as
in global › left.
A declaration or an arrow
Both are functions, and you call both the same way. Two things decide
which one you write. The first is where you can call it from. Lesson
5.1 showed that a function declaration can be called from above its
line. An arrow lives in a const box, and lesson 1.1's rule holds for
every box: it exists only from the line that makes it.
Bad — the call comes first, while
the box left does not exist yet.
Good — the box is made first,
then called.
TypeScript refuses the first with TS2448: Block-scoped variable 'left' used before its declaration. You met this message, for a
box also called left, in lesson 1.1's common mistake, and the
reason is the same. A second message, Variable 'left' is used before being assigned., says the same in other words. If you run it
with Node without checking, Node stops on line 1 with
ReferenceError: Cannot access 'left' before initialization. The
second prints 35.
The second thing is what the function is for. This course follows
one rule from here on:
A named step of your program is a function declaration at
the top of the file: give a task its capital letter, work out how
full the list is. Read top to bottom, the declarations are a list
of what your program can do, and every line below can call them.
A short rule handed to a method is an arrow, written right
where map, filter, find or toSorted asks for it. It is
read in one glance, in the place it is used, and it needs no
name.
Either one works in either place; the rule is there so your code
reads the same everywhere. TaskKit already follows it: capitalize
is a declaration, and the rules for find and toSorted are
arrows. An arrow in a box still turns up in other people's code,
and lesson 5.4 hands one around as a value.
Predict before you run
A body in braces can do more than give back an answer: it can print
what it is doing. The rule handed to this find says which task it
is checking each time find calls it.
Predict
How many times does the rule run?
The common mistake: braces without return
A short arrow grows. It did one thing, it now has to do two, and
two steps need two lines, so you add braces. The short body used to
hand its value back by itself; braces turn that off.
Common mistake
Adding braces to a short body and forgetting return
Your arrow trimmed a task: (task: string): string => task.trim().
Now it should give it a capital letter too, so the work moves into
braces, one step a line:
Check it with tsc (the TypeScript compiler, which npx starts):
npx tsc --noEmit--types node tidy.ts refuses it before it runs:
The first line is one long line in your terminal, broken in three
here to fit the page.
A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
The fix
Hand the value back with return:
It is lesson 5.1's message, met for a new reason, and the return type
caught it again: the wavy line is under string, the promise the body
broke. Without a return type nothing refuses it. Inside map, where
you write no return type,
["buy milk", "call mom"].map((task) => { task.trim(); }) gives back
[ undefined, undefined ]. So the moment you add braces to an arrow,
add return with them.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
Each one runs 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
A TaskKit step tells how full the list is. Which do you use?
Check yourself
You put an arrow's body in braces. What must the body have?
Check yourself
Why does an arrow in a box need a type, but not inside map?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Arrow functions
What things are
Word
What it means
Arrow function
A function written with =>, with no name of its own
Short body
One value after =>, given back with no return
Body in braces
Lines in { }; the value comes back with return
anonymous
The player's name for an arrow that has no box
Code
Write
To
(task) => task.trim()
A rule for a method, which gives task its type
const left = (…) => …
Keep an arrow in a box; call it left(25)
(used: number): number
Its types, as in lesson 5.1
=> { …; return x; }
A body in braces, giving back x
() => "Hi"
An arrow that takes nothing
Rules
Rule
What it means
Inside a method
The method gives the parameter its type
In a box
Write every parameter's type, or TS7006
Box first
Call an arrow only below its line, or TS2448
Braces need return
Without it: TS2355, or undefined
Which one
A named step: function; a rule: an arrow
Where to go next
You can now write a function two ways. You can write an arrow with
typed parameters and a return type, give it a short body or a body
in braces with return, keep it in a box and call it, and you know
why a rule inside map needs no types. You also know which one to
write: a declaration for a named step, an arrow for a rule.
Next is Optional, default, and rest parameters. So far every
call fills every parameter, no more and no fewer. There you let a
call leave a parameter out, give a parameter a value to use when it
is left out, and take any number of arguments.
Before you move on, open index.ts in ~/projects/taskkit and put
the body of the rule you hand to find in braces, with
console.log(`Checking ${task}`); above a return. Check it with
npx tsc --noEmit--types node index.ts, then run
node index.ts add "buy milk" "Buy milk". It prints
Checking Buy milk once, just before Already on the list: Buy milk:
when buy milk was added the list was still empty, so find had
nothing to call the rule on. Put the short body back when you have
seen it.
Exercise · Read the code
Count the calls filter makes
How many Looking at lines does this program print?
function left(used: number): number { return 60 - used;}
Then: left
Outputtext
tidy.ts:1:30 - error TS2355: A function whosedeclared type is neither 'undefined', 'void', nor'any' must return a value.1 const tidy = (task: string): string => { ~~~~~~Found 1 error in tidy.ts:1
left.ts
const left = (used: number): number=> 60 - used;
What follows it
left.tsts
const left = (used: number): number => 60 - used;console.log(left(25));console.log(left(50));
map calls the arrow for every item, and keeps each answer
Exercise · Fix the error
Hand the minutes back
This program should print the minutes left in the hour after each
task: [ 35, 20, 50 ]. Neither TypeScript nor Node complains, yet
it prints [ undefined, undefined, undefined ]. Fix the arrow.
Loading the editor…
Exercise · Fix the error
Make the box before the call
TypeScript refuses this program with Block-scoped variable 'hours' used before its declaration. Change the order of the lines,
without changing any line itself, so it prints 1.5.
Loading the editor…
Exercise · Write the code
Write taskLine as an arrow
Code written by other people often keeps a function in a box.
Rewrite lesson 5.1's taskLine as an arrow function kept in a
const box, with the same parameter types and the same return
type. The program still prints buy milk: 25 min.