Give a type a name with type and write the name wherever the type goes: on boxes, lists, parameters and results. See that the check opens the name, that messages say Task instead of the whole outline, and that an alias is only a name, so Minutes and TaskId are both number. TaskKit describes a task once, as Task.
Fundamentals
TypeScript
Type aliases (`type`). Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of the last lesson, TaskKit's tasks got an id (short for
identifier, the number that names a task) nobody can change and a due date
they may leave out. To get there you typed the same outline twice: { readonly id: number; title: string; due?: string } on the list, and again on
printList. One day a task will need a done, and you will have to find
every copy and change each one the same way. Miss one, and two parts of
TaskKit disagree about what a task is.
Your phone's contacts solved this long ago, in a second way. You
do not type your mother's
number every time you call her: you saved it once, under "Mom", and
now you call "Mom". When she gets a new number, you change it in
one place, and every call after that goes to the new one. In this
lesson you do the same for a type. By the end you will give the
outline of a task one name, Task, write it once, and use the name
everywhere the outline stood. TypeScript's messages will say Task
too, instead of reading the whole outline back to you.
One outline, written again and again
Here are the two copies in TaskKit's index.ts, as the last lesson
left them:
Nothing ties the two together. They are the same today only
because you typed them the same, and every new function that takes
a task would add a third copy. Copies drift: someone adds a property
to one and forgets the other. Run this small version, where the
function's copy has a done that the box's copy does not:
TypeScript refuses the last line with Property 'done' is missing in type '{ id: number; title: string; }' but required in type '{ id: number; title: string; done: boolean; }'.: both copies,
quoted whole, and you are left to spot the one property that
differs.
With two short outlines that is easy. With five properties each, in
two files, it is not.
A type alias: one name for a type
A type alias is a name you give to a type, so you can write the
name instead of the type. You make one with the keyword type, the
name, =, and the type it stands for:
Read it as "Task means this outline". The line makes no box and
holds no value: it is a note for TypeScript, which from now on reads
Task as the outline written after the =.
Two details are worth a closer look. The = is the sign from lesson 2.1 in a
new place: after const it puts a value in a box; after type it says what a
name means. The colon you know from lesson 2.2 says what type a box has:
const task: Task reads "the box task holds a Task". And the name starts
with a capital letter. The basic types stay in lower case, as lesson 2.2 said,
but a type you name yourself starts with a capital, so a type, Task, never
looks like a box, task. It is camelCase from lesson 2.7, first letter
included: TaskId. TypeScript does not insist on it; most code you will read
does it, and so does this course.
Once the name exists, it goes everywhere the outline went: after a
box's colon, in front of [] for a list, on a parameter, and as a
function's return type, after the ): function newTask(id: number, title: string): Task. The program below uses the first three; the
last exercise gives back a Task. Run it:
It prints #1 Pay rent, due: Friday and #2 Call Mom, due: not set. The outline is written once, on line 1, and used three times.
Rest the pointer on tasks in VS Code (Visual Studio Code, your
editor) and it shows Task[], the
name, not the outline.
Now the copies cannot drift, because there is only one. Add
done: boolean to the line that makes Task, and every place that
uses the name asks for a done at once: here both tasks are refused
until they have one. One change, and TypeScript finds every value it
affects.
The check opens the name
A name changes nothing about how a value is checked. When TypeScript
meets Task after a colon, it looks up what Task means and holds
the value against that outline, one property at a time, exactly as
in the last two lessons. Here Task checks three values:
{ id: 1, title: "Pay rent" }, { id: "1", title: "Pay rent" } and
{ id: 1 }. 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
Task: reachedOpened into the outline it names
readonly id: number: passes✓Is id there, and a number?
title: string: passes✓Is title there, and text?
due?: string: passes✓Left out, or there and text?
Node: reachedRuns every line
TypeScript opens the name, then checks the value as before
All 9 steps shown.Step 1 of 9: The valueStep 2 of 9: TaskStep 3 of 9: readonly id: numberStep 4 of 9: readonly id: number: passesStep 5 of 9: title: stringStep 6 of 9: title: string: passesStep 7 of 9: due?: stringStep 8 of 9: due?: string: passesStep 9 of 9: Node
All 9 steps shown.
The value: reachedThe object you wrote
Task: reachedOpened into the outline it names
readonly id: number: stops here✕Is id there, and a number?
title: string: never reachedIs title there, and text?
due?: string: never reachedLeft out, or there and text?
Node: never reachedRuns every line
readonly id: number stops hereType 'string' is not assignable to type 'number'.
TypeScript opens the name, then checks the value as before
All 4 steps shown.Step 1 of 4: The valueStep 2 of 4: TaskStep 3 of 4: readonly id: numberStep 4 of 4: readonly id: number: stops here. Type 'string' is not assignable to type 'number'.
All 4 steps shown.
The value: reachedThe object you wrote
Task: reachedOpened into the outline it names
readonly id: number: passes✓Is id there, and a number?
title: string: stops here✕Is title there, and text?
due?: string: never reachedLeft out, or there and text?
Node: never reachedRuns every line
title: string stops hereProperty 'title' is missing in type '{ id: number; }'
but required in type 'Task'.
TypeScript opens the name, then checks the value as before
All 6 steps shown.Step 1 of 6: The valueStep 2 of 6: TaskStep 3 of 6: readonly id: numberStep 4 of 6: readonly id: number: passesStep 5 of 6: title: stringStep 6 of 6: title: string: stops here. Property 'title' is missing in type '{ id: number; }'
but required in type 'Task'.
All 6 steps shown.
Run the one with no title:
The message, TS2741 (TS for TypeScript, then the number of this
kind of mistake), reads in the same three parts as in lesson 6.1,
and its last part is shorter. After but required in type it says
'Task', where without the name it would have quoted the whole
outline. Here is the same mistake both ways: first unnamed,
const task: { readonly id: number; title: string; due?: string } = { id: 1 }; on line 1, then with Task. Each card is what
npx tsc --noEmit--types node task.ts prints first (tsc is short
for TypeScript compiler, which npx starts), broken here to fit
the card:
task.ts:1:7 - error TS2741:
Property 'title' is missing in
type '{ id: number; }' but
required in type '{ readonly
id: number; title: string;
due?: string | undefined; }'.
The outline written out
Never: You read the whole outline to see which type it was
The type after required in type is the whole outline, copied
from the code, with | undefined added for the ?.
task.ts:2:7 - error TS2741:
Property 'title' is missing in
type '{ id: number; }' but
required in type 'Task'.
The outline named Task
Does: It says which type the value had to fit
The same mistake. The message ends with the name, so you know
which outline to open.
Unnamed, the message quotes the outline; named, it says Task
When a message names a type you made, read the note tsc prints after
it in the terminal, 'title' is declared here., which points at the
line that makes the type; or rest the pointer on the name in VS Code,
which shows the outline it stands for. The property the message names
is in there.
A name for any type
type can name any type, not only an outline. A number of minutes
and a task's id are both plain numbers, but the names say which is
which:
function plan(minutes: Minutes) now tells whoever reads it what
the number means. What it does not do is make a new kind of number.
An alias is only another name: Minutesisnumber, and so is
TaskId, so every value that fits one fits the others:
The type15"15"number
Fits
15 is a number.
Refused:TS2322
Text is not a number.
Minutes
Fits
Minutes means number.
Refused:TS2322
Text is not a number.
TaskId
Read it by rows: the three rows come out the same, so a 15 meant as minutes
fits where an id is wanted. So an alias is there for the reader of the code,
not as a guard. Keeping minutes out of an id takes more than a name, and a
later level shows how.
TaskKit: one Task, written once
Now TaskKit's two copies become one name.
1
Name the outline
In index.ts in ~/projects/taskkit, add this line under the
import line at the top:
2
Use the name in both places
The first lines of printList become one line:
and the line that makes the list becomes:
The rest of the file stays as it is.
3
Check, run TaskKit, run the tests
npx tsc prints nothing: the check passed. node --test still
counts pass 7.
The output is exactly what it was in the last lesson, and again
that is the point. What changed is where a task is
described: in one line, at the top, under a name. Every function
you add to TaskKit from now on takes a Task, and the day a task
gets a new property, you write it once.
Predict before you run
Two aliases for numbers, and a function that finds a task's title
by its id. The program passes it a number of minutes. Decide what
happens before you run it.
Predict
What does this program print?
The common mistake: a colon where = belongs
You have spent two lessons writing a colon before every outline:
const task: { … }, task: { … }. So the first alias you write
often gets one too.
Common mistake
Writing type Task: instead of type Task =
The name, then a colon, then the outline, as after a box:
'=' expected.
The fix
An alias says what the name means, so it takes =:
The first message is short and points at the colon: after type Task,
TypeScript expects = and nothing else. Two more can follow on the
same line, Declaration or statement expected. and Expression expected.; they all come from the same colon, and go when it does. If
you run the file without checking it first, Node stops too, with
Unexpected token `:`. Expected =. Keep the two marks apart: a
colon gives a box or a parameter its type, and = gives a type its
name.
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 does type Task = { title: string }; do?
Check yourself
A TaskId box gets the text 2, in quotes. What happens?
Check yourself
tsc says: required in type 'Task'. Where is the property?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Type aliases
What things are
Word
What it means
Type alias
A name for a type, written once with type
= in an alias
Says what the name means
: after a box
Says what type the box has
Code
Write
To
type Task = { title: string };
Name an outline
const task: Task = …
Give a box that type
Task[]
Type a list of tasks
(task: Task)
Type a parameter
): Task {
Type what a function gives back
type Minutes = number;
Name any type, not only outlines
Rules
Rule
What it means
One place
Change the alias, and every use follows
A capital letter
Task for a type, task for a box
Same type
Minutes is number: any number fits
Messages
Say 'Task', not the whole outline
Node alone
Skips every type line
type Task: {
'=' expected.
Where to go next
You can now give any type a name with type, use that name on
boxes, lists, parameters and results, and read a message that names
your type instead of quoting it. You also know what a name does not
do: it makes no new type, so Minutes and TaskId are both just
number. TaskKit describes a task once, in one line, as Task.
Next is Interfaces and extends. There you meet a second way
to name an object's outline, interface, and build a bigger outline
on top of one you already have with extends.
Before you move on, open TaskKit, add done: boolean to the line that makes
Task, and run the npx tsc line from the steps above. Read where TypeScript
stops and why: one change, and it found the one line that builds a task. Then
take done out again.
Fits
TaskId means number.
Refused:TS2322
Text is not a number.
The value
Minutes and TaskId are names for number: the same values fit
Exercise · Read the code
Which line names the outline
Which of these lines gives the outline { title: string } the name
Task?
Outputtext
============================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
This program should print Pay rent: 15 min (min is short for
minutes), but TypeScript refuses
its first line. Fix that line so Task names the outline, and leave
the other two as they are.
Loading the editor…
Exercise · Write the code
Name the outline and use it
The outline { title: string; minutes: number } is written twice
below. Give it the name Task, write Task in both places, and
finish describe so it gives back the title with the minutes in
parentheses: "Pay rent (15 min)" for the first task. The program
then prints Pay rent (15 min) and Call Mom (10 min). The line that
makes next already uses Task, so TypeScript refuses it until the
name exists.
Loading the editor…
Exercise · Write the code
Give back a Task
Finish makeTask, which takes an id and a title and gives back a
Task with both: makeTask(1, "Pay rent") gives back
{ id: 1, title: "Pay rent" }, with no due date.