Name an object’s outline with interface and build a bigger one on top of it with extends: a PlannedTask is a Task plus minutes. See that an interface is checked like an alias, that TS2739 lists the missing properties, and that a PlannedTask fits wherever a Task is wanted, never the other way round.
Fundamentals
TypeScript
Interfaces and `extends`. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of the last lesson, TaskKit described a task once, in one
line, under the name Task. Now you want to plan your day: pick a
few tasks for today and say how many minutes each one will take, so
you can see whether the day fits. A planned task is a task with one
more fact about it. It has an id (short for identifier, the
number that names one task), a title and maybe a due date, like
every task, and it also has its minutes.
Paper forms work the same way. A hotel's booking form asks for what every
booking has, a name and the dates, and its group booking form asks for all of
that plus the number of guests. Nobody writes the first form out again: the
group form is the booking form, plus one line. In this lesson you do that for
a type. By the end you will write a planned task as "a Task, plus minutes"
in three lines, and hand a planned task to any function that takes a task.
A second way to name an outline
type is not the only way to give an outline a name. TypeScript has
a second keyword, made only for the outlines of objects:
interface. An interface is a named outline for an object. You
write interface, the name, and the outline straight after it:
Read it as "a Task is an object with these properties". Compared
with last lesson's type Task = { … };, two marks are gone: there is
no = between the name and the {, and no ; after the closing }.
Inside the braces the properties are the same, with the same
readonly and ?. Code you read usually puts each property of an
interface on a line of its own, ending with a semicolon, the last one
too, and so does this course's code; a short answer or a table row may
fit one on a line.
You use the name of an interface exactly as you used an alias. It goes
after a colon, in front of [], on a parameter and as a return type,
and TypeScript holds a value against it one property at a time. Here
is the same mistake, a task with no title, against both:
As a type alias
Does: type, the name, =, then the outline
This lesson: interface
Does: interface, the name, then the outline: no =
type and interface name the same outline, checked the same way
Both messages read, word for word, Property 'title' is missing in type '{ id: number; }' but required in type 'Task'. Here is last
lesson's program, with Task written as an interface. Run it:
It prints #1 Pay rent, due: Friday and #2 Call Mom, due: not set, as before. Like a type line, an interface belongs to
TypeScript: Node skips it, and the file runs with node as it is.
Which of the two should you write? For an object's outline both do
the job, and the next lesson gives you a simple rule for choosing.
This lesson uses interface, because its second half, extends, is
written on one.
Build on an outline: extends
A planned task needs everything a Task has, and minutes. With
what you know so far, there are two ways to get there, and both cost
something.
The first is to add minutes?: number to Task itself. Then every
task may have minutes, the ones you never planned included, and a
loop that adds them up with total += task.minutes is refused with
'task.minutes' is possibly 'undefined'., the message you know from
lesson 3.3. The second is to write the planned task's outline out in
full:
Bad — copies Task's outline into a
second interface, so there are two copies to keep the same.
Good — builds PlannedTask on Task,
and writes only what it adds.
Both print Call Mom today. The Bad version is last lesson's
problem back again: when Task gets a done one day, PlannedTask
does not, and nothing on those lines tells you. Two outlines that
should agree have drifted apart, and you find out only when a
planned task meets a function that wants the new done.
extends is the word that builds one interface on top of another.
Read interface PlannedTask extends Task as "a PlannedTask is a
Task, plus what is between these braces". PlannedTask gets every
property Task has, readonly and ? included, and adds its own
minutes. Since Task's properties are written in one place only,
a change to Task is a change to PlannedTask too.
How a planned task is checked
When TypeScript meets PlannedTask after a colon, it opens the
name, as in the last lesson. This time it finds two parts: all of
Task, and minutes. A value has to pass both. Switch between the
three values to see where each one stops, and press Next step to
walk through it, or let it play:
The value: reachedThe object you wrote
PlannedTask: reachedOpened: Task, plus minutes
Task: passes✓Does it fit all of Task's outline?
minutes: number: passes✓Is minutes there, and a number?
Node: reachedRuns every line
PlannedTask is checked for all of Task first, then for minutes
All 7 steps shown.Step 1 of 7: The valueStep 2 of 7: PlannedTaskStep 3 of 7: TaskStep 4 of 7: Task: passesStep 5 of 7: minutes: numberStep 6 of 7: minutes: number: passesStep 7 of 7: Node
All 7 steps shown.
The value: reachedThe object you wrote
PlannedTask: reachedOpened: Task, plus minutes
Task: passes✓Does it fit all of Task's outline?
minutes: number: stops here✕Is minutes there, and a number?
Node: never reachedRuns every line
minutes: number stops hereProperty 'minutes' is missing in type '{ id: number;
title: string; }' but required in type 'PlannedTask'.
PlannedTask is checked for all of Task first, then for minutes
All 6 steps shown.Step 1 of 6: The valueStep 2 of 6: PlannedTaskStep 3 of 6: TaskStep 4 of 6: Task: passesStep 5 of 6: minutes: numberStep 6 of 6: minutes: number: stops here. Property 'minutes' is missing in type '{ id: number;
title: string; }' but required in type 'PlannedTask'.
All 6 steps shown.
The value: reachedThe object you wrote
PlannedTask: reachedOpened: Task, plus minutes
Task: stops here✕Does it fit all of Task's outline?
minutes: number: never reachedIs minutes there, and a number?
Node: never reachedRuns every line
Task stops hereType '{ minutes: number; }' is missing the following
properties from type 'PlannedTask': id, title
PlannedTask is checked for all of Task first, then for minutes
All 4 steps shown.Step 1 of 4: The valueStep 2 of 4: PlannedTaskStep 3 of 4: TaskStep 4 of 4: Task: stops here. Type '{ minutes: number; }' is missing the following
properties from type 'PlannedTask': id, title
All 4 steps shown.
Run the two that stop:
The first message is one you know: minutes is missing, and the type
it is missing from is PlannedTask. The second is new, TS2739 (TS
for TypeScript, then the number of this message): Type '{ minutes: number; }' is missing the following properties from type 'PlannedTask': id, title. When two to five properties are missing,
TypeScript lists them all in one message, after the colon; with six or
more it lists the first four and says how many more, under its own
number, TS2740.
The figure checks Task's part first only to show the two parts apart.
TypeScript checks the whole outline at once: a value with neither part is told
is missing the following properties from type 'PlannedTask': minutes, id, title, PlannedTask's own property first.
Look at which type it names. You wrote id and title on Task, yet
the message says 'PlannedTask', because they are as much a part of
PlannedTask as minutes is. A message about a missing property
always names the type the value had to fit. To find where a property
it lists was written, open that type; if the property is not there,
open the type after its extends.
Once a value passes, every planned task is sure to have its
minutes, so adding them up needs no question about each one:
It prints Today: 25 min (min is short for minutes). The line total += task.minutes is the one an optional minutes? would have refused.
A planned task is still a task
PlannedTask has every property Task asks for. So a planned task fits
anywhere a task is wanted: a function that takes a Task reads what Task
has, its id, its title and maybe its due, and a planned task has every
one of them. The other way round does not work. A plain Task has no
minutes, so it cannot go where a PlannedTask is wanted. Read the grid by
rows:
Where it goesA TaskA PlannedTaskTask wanted
Fits
Its own type.
Fits
It has id and title too.
PlannedTask wanted
Refused:TS2741
It has no minutes.
Fits
Its own type.
The box you pass
A PlannedTask fits where a Task is wanted; a Task lacks minutes
So every function you already wrote for a Task takes a planned
task as it is. And because PlannedTask is built on Task, it
keeps fitting there when Task changes: there is only one outline
to change. Run both calls:
TypeScript refuses only the last line, with Property 'minutes' is missing in type 'Task' but required in type 'PlannedTask'. Delete
that line, and the program prints #3 Buy milk.
TaskKit stays as it is in this lesson: it has no planned tasks yet,
and its type Task is still right. Which of the two words its
Task should use is the next lesson's question.
Predict before you run
A planned task goes into a function that takes a plain task, and
the program then prints its minutes. Decide what the second line
says before you run it.
Predict
What is the second line this program prints?
The common mistake: an = in an interface
You spent the last lesson writing type Task = {. So the first
interface you write often keeps the =.
Common mistake
Writing interface Task = { instead of interface Task {
The name, then =, then the outline, as in an alias:
'{' expected.
The fix
An interface takes its outline straight after the name, with no =
and no ; after the }:
The first message points at the =: after interface Task,
TypeScript expects { and nothing else. One or two more messages
can follow on the lines under it, such as Declaration or statement expected.; they come from the same =, and go when it does. Node
alone stops too, with Expected '{', got '='. Keep the two shapes
apart: type Task = { and interface Task {.
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
A value fails type Task = { title: string }. Does it
pass interface Task { title: string }?
Check yourself
What must a PlannedTask that extends Task have?
Check yourself
A function takes a Task. Can it take a PlannedTask?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Interfaces and extends
What things are
Word
What it means
Interface
A named outline for an object, written with interface
extends
Builds one interface on top of another
Code
Write
To
interface Task { title: string; }
Name an object's outline
interface PlannedTask extends Task { … }
Build an outline on Task
const task: Task = …
Give a box that type, as with an alias
Rules
Rule
What it means
No =
interface Task = { is refused: '{' expected.
Same check
An interface is checked like an alias
All of Task
A PlannedTask needs id, title and minutes
Several missing
TS2739: is missing the following properties
Bigger fits smaller
A PlannedTask goes where a Task is wanted
Not the other way
A Task where a PlannedTask is wanted: TS2741
Node alone
Skips every interface line
Where to go next
You can now name an object's outline with interface, build a
bigger outline on top of it with extends, and read the message
that lists every property a value is missing. You also know which
way round it fits: a planned task goes wherever a task is wanted,
and a plain task does not go where a planned one is.
Next is type or interface: a simple rule for choosing. You
now have two ways to name an object's outline, and for most outlines
they do the same job. There you get one rule for which to write, and
TaskKit's Task follows it.
Before you move on, take any code block on this page with Task in
it and build a ShoppingTask of your own on top of it, with
shop: string. Make one, put it in a box, and pass it to a function
that takes a Task. Then leave out its title and read the whole
message, down to the type it names.
Exercise · Read the code
Which line builds on Task
Task is an interface already. Which of these lines makes
PlannedTask: every property of Task, plus minutes?
This program should print #4 Water plants, but TypeScript refuses
its first line. Fix the interface, and leave the other lines as they
are.
Loading the editor…
Exercise · Fix the error
Give the planned task all it needs
This program should print #2 Call Mom: 10 min, but TypeScript
refuses the line that makes call. Fix that line so call is
task 2, Call Mom, taking 10 minutes, and leave the rest as it is.
Loading the editor…
Exercise · Write the code
Describe a planned task
Task is written for you. Add PlannedTask, a Task plus
minutes: number, built with extends, and finish describePlanned
so it gives back the id, the title and the minutes:
#1 Pay rent: 15 min.