Structural typing: shapes, not names; excess property checks
See that TypeScript checks an object by its shape, its properties and their types, never by the name of its type, and why an object literal written straight into a call or a typed box may not bring a property the type does not know. Fix a misspelt optional property instead of hiding it in a box.
Fundamentals
TypeScript
Structural typing: shapes, not names; excess property checks. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Two lessons ago, a callout left you with a puzzle. A planned task kept
in a box went into describe, a function that takes a plain Task,
and TypeScript said nothing. Written straight into the call instead,
describe({ id: 3, title: "Buy milk", minutes: 5 }), the same task
was refused. Same id (short for identifier, the number that names
one task), same title, same minutes. The only thing that changed was
where the object was written.
To see why, you need to know what TypeScript actually looks at when it
checks a value. A recipe is a good picture of it. It asks for two eggs
and a cup of flour. It does not care whether the eggs came in a carton
with a shop's brand on it or in a bag from a neighbour, and it does
not mind that the bag also holds an apple. By the end of this lesson
you can tell which values fit an outline, whatever their type is
called, and you know the one place where TypeScript asks for more.
TypeScript compares shapes, not names
Take TaskKit's Task, with its id, title and optional due, and
the describe function from lesson 6.4 that prints the due date the
way TaskKit does. A calendar app might keep its own outline with the
same three properties, under a name of its own: Reminder.
describe asks for a Task. What does TypeScript need to see before
it lets a value in? Not the word Task. It goes down the outline one
property at a time, the check you met in lesson 6.1, and asks of each
property: is it there, and of the right kind? None of the questions
asks what the value's type is called. Switch between four values, a
Task, a Reminder, an object with no type written, and one with no
id, and press Next step to walk through each one, or let it
play:
The value: reachedPassed to describe
id: number: passes✓Is id there, and a number?
title: string: passes✓Is title there, and text?
due?: string: passes✓If due is there, is it text?
describe: reachedRuns with the value
TypeScript asks about the properties, never about the
type's name
All 8 steps shown.Step 1 of 8: The valueStep 2 of 8: id: numberStep 3 of 8: id: number: passesStep 4 of 8: title: stringStep 5 of 8: title: string: passesStep 6 of 8: due?: stringStep 7 of 8: due?: string: passesStep 8 of 8: describe
All 8 steps shown.
The value: reachedPassed to describe
id: number: passes✓Is id there, and a number?
title: string: passes✓Is title there, and text?
due?: string: passes✓If due is there, is it text?
describe: reachedRuns with the value
TypeScript asks about the properties, never about the
type's name
All 8 steps shown.Step 1 of 8: The valueStep 2 of 8: id: numberStep 3 of 8: id: number: passesStep 4 of 8: title: stringStep 5 of 8: title: string: passesStep 6 of 8: due?: stringStep 7 of 8: due?: string: passesStep 8 of 8: describe
All 8 steps shown.
The value: reachedPassed to describe
id: number: passes✓Is id there, and a number?
title: string: passes✓Is title there, and text?
due?: string: passes✓If due is there, is it text?
describe: reachedRuns with the value
TypeScript asks about the properties, never about the
type's name
All 8 steps shown.Step 1 of 8: The valueStep 2 of 8: id: numberStep 3 of 8: id: number: passesStep 4 of 8: title: stringStep 5 of 8: title: string: passesStep 6 of 8: due?: stringStep 7 of 8: due?: string: passesStep 8 of 8: describe
All 8 steps shown.
The value: reachedPassed to describe
id: number: stops here✕Is id there, and a number?
title: string: never reachedIs title there, and text?
due?: string: never reachedIf due is there, is it text?
describe: never reachedRuns with the value
id: number stops hereProperty 'id' is missing in type '{ title: string; }'
but required in type 'Task'.
TypeScript asks about the properties, never about the
type's name
All 3 steps shown.Step 1 of 3: The valueStep 2 of 3: id: numberStep 3 of 3: id: number: stops here. Property 'id' is missing in type '{ title: string; }'
but required in type 'Task'.
All 3 steps shown.
This file passes describe three values that are not Tasks by name.
The first is a Reminder, the second has no type written at all, and
the third carries minutes, which Task never mentions. The figure's
last value, one with no id, is the one TypeScript stops. Run it:
It prints all three: #4 Dentist, due: Monday, then
#2 Call Mom, due: not set and #3 Buy milk, due: not set. Each of
them has an id that is a number and a title that is text, and
the one with a due has it as text. That is all a Task asks for.
This way of checking is called structural typing: types are
compared by their structure, not by their names. The structure of a
value is its shape: which properties it has, and the type of
each. Two outlines with the same properties accept the same
values. It is also why a planned task fitted where a
task was wanted in lesson 6.4. It was never the extends that let it
in: a PlannedTask has an id and a title, and that is enough.
The one thing that does not count is the type's name. The names of
the properties are the shape itself, so an object with name where
the outline says title has no title, whatever the word means to
you.
An object literal may bring nothing extra
Back to the puzzle. milk above carried minutes and went through.
Now write the same object straight into the call:
TypeScript stops at the last line with TS2353 (TS for TypeScript,
then the number of this message): Object literal may only specify known properties, and 'minutes' does not exist in type 'Task'. Lesson
2.6 called a value written straight into the code a literal, like
"0.1" or 5. An object written out in braces right where it is used
is an object literal, and that is what the message is about. When
an object literal meets a type, here the Task that describe asks
for, TypeScript asks one more question: does it bring anything the
type does not know? This is the excess property check; excess
means more than is needed.
The object literal that filled milk met no type: milk has none
written. In the call, describe gets the box milk, not the object
literal, so only the property questions are asked.
Why the difference? Ask what could ever read the extra. milk is a
box: after the call, a later line can still read milk.minutes, so
the extra has a use. An object literal written into the call has no
box. The only code that will ever see it is describe, and through a
Task nothing can read minutes (inside describe, task.minutes
is refused with lesson 6.2's TS2339). A property that no line can
ever read is almost always a mistake, so TypeScript stops it. Switch
between the two, and press Next step to walk through each, or let
it play:
The figure asks the two questions one after the other to keep them
apart. For an object literal TypeScript asks about extras first:
describe({ id: 2, name: "Call Mom" }) is told
'name' does not exist in type 'Task', not that title is missing.
The same check runs wherever an object literal meets a type. In a
box with a type, const milk: Task = { id: 3, title: "Buy milk", minutes: 5 }; is refused with the same message, because that box
lets later lines read only what a Task has. In a list typed
Task[], every object written inside the brackets is checked the
same way.
When the extra is meant, say so in the type. A task that needs its
minutes is a planned task, so give its box that type:
It prints #3 Buy milk, due: not set and Buy milk: 5 min (min is
short for minutes). The object literal is checked against
PlannedTask, which knows minutes. Then milk goes into describe
as a box, by its shape.
Put together, the two checks make one rule. A missing property is
refused wherever the object comes from, because a Task promises it,
and anything that takes a Task may read it; an extra one only in an
object literal that meets a type. "Kept in a box first" in the grid
means a box with no type written, like milk in shapes.ts, or one
whose type knows the extra, like milk: PlannedTask. Read the grid by
rows:
Its propertiesWritten straight inKept in a box firstOnly Task's
Fits
All known.
Fits
All known.
One missing
Refused:TS2741
Always checked.
Refused:TS2741
Always checked.
One extra
Predict before you run
A bill from a banking app has an id, a title, an amount and a due
date, and it goes into describe. Decide what the last line prints
before you run it.
Predict
What does the last line print?
The common mistake: a misspelt due
due is optional, so a task without it fits Task, and a misspelt
due is easy to miss: without a check, the task would still fit, only
with no date.
Common mistake
Writing dew instead of due in an object literal
You give the rent a due date, and type the word the way it sounds:
Object literal may only specify known properties, and 'dew' does not exist in type 'Task'.
The fix
Write the property exactly as the outline names it:
When the message names a property, compare it letter by letter with
the outline. As with lesson 6.1's Did you mean, a close misspelling
gets a suggestion: titel gets TS2561, which ends with Did you mean to write 'title'?.
Do not make the message go away by putting the object in a box
first. const draft = { id: 1, title: "Pay rent", dew: "Friday" }; and then const rent: Task = draft; passes, and
describe(rent) prints #1 Pay rent, due: not set.
Pass all three 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 goes where a Task is wanted. What is checked?
Check yourself
Why may milk carry minutes in, but the object literal not?
Check yourself
'dew' does not exist in type 'Task'. What do you do?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Structural typing and excess property checks
What things are
Word
What it means
Structural typing
Types compared by their properties, not their names
Shape
Which properties a value has, and the type of each
Object literal
An object written out in braces where it is used
Excess property check
An object literal may bring nothing its type lacks
Code
Write
What happens
describe(dentist)
A Reminder fits: the same properties
describe(milk)
A box with no type written fits, extras and all
describe({ … })
An object literal: an extra is TS2353
const milk: Task = { … }
A typed box: an extra is TS2353 too
const milk: PlannedTask
Say in the type that the extra is meant
Rules
Rule
What it means
Shapes, not names
Any value with the properties fits
Property names count
name is not title: no title, refused
A missing property
Refused everywhere: TS2741
An extra property
Refused only in an object literal that meets a type
Close misspelling, literal
TS2561: Did you mean to write 'title'?
Never hide it in a box
The message goes; the mistake stays
Node alone
Where to go next
You can now tell whether a value fits an object type by looking at
its properties alone, whatever its type is called. You also know the
one place where TypeScript asks for more: an object literal may not
bring a property the type does not know, because nothing could ever
read it.
Next is References vs copies: why changing one object changes
another. There you find out what really happens when you hand
milk to a function, or put it in a second box: whether the
function gets its own copy of the task, or the very same one.
Before you move on, go back to shapes.ts, press Edit, and add
a thing from your own life with an id and a title: a bill, a
birthday, a library book, with one property of its own. Pass it to
describe once from a box and once written straight into the call,
and predict what each one does before you press Run.
Yourcode
TypeScript
Node
Screen
Your code: { … } in the call{…}inthecall{…}inthecall
TypeScript: id ✓ title ✓id✓title✓id✓title✓
TypeScript: 'minutes' does not exist in type 'Task'.'minutes'doesnotexistintype'Task'.'minutes'doesnotexistintype'Task'.
A new object, written inside the call: an id, a title, and
minutes.
Both properties Task requires are there, of the right kind.
TypeScript stops at the extra, before anything runs.
Node never starts, so not one line of the program runs.
Nothing reaches the screen.
Written into the call, minutes is refused; kept in a box,
it stays with milk
All 5 steps shown.Step 1 of 5: Your code: { … } in the callStep 2 of 5: TypeScript: id ✓ title ✓Step 3 of 5: TypeScript: 'minutes' does not exist in type 'Task'.Step 4 of 5: Node: never startsStep 5 of 5: Screen: nothing printed
All 5 steps shown.
Yourcode
TypeScript
Node
Screen
Your code: milk in the callmilkinthecallmilkinthecall
TypeScript: id ✓ title ✓id✓title✓id✓title✓
TypeScript: checked ✓checked✓checked✓
Node: runs describerunsdescriberunsdescribe
Screen: #3 Buy milk, due: not set#3Buymilk,due:notset#3Buymilk,due:notset
Time
The same object, made a line earlier and kept in a box called
milk.
Both properties Task requires are there, of the right kind.
milk is a box, so only Task's properties are checked.
Node runs the file, and describe reads the id and the title.
The task is printed, and milk still holds its 5 minutes.
Written into the call, minutes is refused; kept in a box,
it stays with milk
All 5 steps shown.Step 1 of 5: Your code: milk in the callStep 2 of 5: TypeScript: id ✓ title ✓Step 3 of 5: TypeScript: checked ✓Step 4 of 5: Node: runs describeStep 5 of 5: Screen: #3 Buy milk, due: not set
All 5 steps shown.
Refused:TS2353
Nothing can read it.
Fits
The box keeps it.
The object
A missing property is refused everywhere; an extra one only
in an object written straight in
Never compares an object with an outline
Exercise · Read the code
Which line is refused
Task and describe are the ones from this lesson, dentist is the
Reminder from shapes.ts, and this box is made first:
This program should print At the corner shop: 2, but TypeScript
refuses it with four messages. All four have one cause, the line that
makes the list: it says Task[], and the tasks in it name a shop.
Three messages point at the tasks, the fourth at the countAt call.
Fix the line that makes the list, and leave the rest as it is.
Loading the editor…
Exercise · Write the code
An Appointment for describe
Task and describe are written for you. Write an interface
Appointment with readonly id: number, title: string and
place: string, make a box checkup of that type for task 4,
Dentist, at Main St, and pass it to describe, so the program
prints #4 Dentist, due: not set.