Tell objects of different shapes apart with the in operator, like cards on the fridge where only a chore card has a space for minutes. See which type TypeScript gives the value on each side of the check, add up a week of chores, learn why an optional property lands on both sides, and why !== undefined cannot be the check.
Fundamentals
TypeScript
Narrowing with the `in` operator. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
By Sunday the sheet on the fridge says more than a number. Every day
has a card now. Monday's card says dishes, 30 minutes; Tuesday's says
laundry, 45 minutes. Wednesday's card names no chore and no minutes,
only a reason: sick. You add total += entry.minutes; to the loop
over the cards, and TypeScript refuses it. You reach for lesson 7.3's
typeof, and it says "object" for every card.
Look at the cards themselves, though, and you can tell them apart at a
glance: only a chore card has a space for minutes. In this lesson you
ask that question in code, with the in operator. By the end you can
tell two shapes of object apart and say which type TypeScript gives
the value on each side of the check, and the week adds up. You also
see why a property that may be missing can send a chore to the wrong
side of the check, and why !== undefined cannot be the check here.
Two shapes of object in one union
In code, each card is an object, and each kind of card gets its own
outline, an interface. A union can join any types, interfaces
included, so one type covers every card on the sheet. Entry is
written with type, because a union is not an object's outline
(lesson 6.5); in lessons 7.1 and 7.3 it was string | number. Press
Run:
TypeScript stops it before anything runs: Property 'minutes' does not exist on type 'Entry'. The second line of the message, Property 'minutes' does not exist on type 'DayOff'., names the member that
said no, as in lesson 7.1: a line is allowed on a union only when
every member allows it. Line 19 is allowed, because both types have
day. Line 20 is not, because a DayOff has no minutes.
Ask whether a property is there
The in operator asks an object one question: do you have a
property with this name? You write the name in quotes on its left and
the object on its right, and it gives back true or false. The
right side must be an object: "minutes" in 30 is refused with Type 'number' is not assignable to type 'object'., where object is
TypeScript's name for any object, the word typeof gives for one in
lesson 7.3. Like typeof, it is JavaScript, so it asks while the
program runs, about the object in the box at that moment:
It prints true, false and true. The quotes matter: the left side
is the property's name, written as text. Without them, minutes in monday reads minutes as the name of a box, and TypeScript stops
with TS2304, Cannot find name 'minutes'., the message lesson 1.1
showed for a missing pair of quotes.
in as a check
Put in in an if, and TypeScript narrows the type, as it did for
typeof in lesson 7.3. Inside the check it keeps every member of the
union that has the property; in the else, every member that does
not. Here only a Chore has minutes, so each branch knows which
shape it got. Press Next branch to walk through them, or let it
play:
entry:Chore | DayOff
If "minutes" in entry, entry is Chore"minutes" in entry→Choreentry.minutes is allowed
Otherwise, entry is DayOffelse→DayOffentry.reason is allowed
"minutes" in entry: each branch knows which shape it got
All 2 branches shown.Branch 1 of 2: If "minutes" in entry, entry is ChoreBranch 2 of 2: Otherwise, entry is DayOff
All 2 branches shown.
Now the week adds up. Press Run:
It prints Monday: dishes, 30 min (min for minutes), Tuesday: laundry, 45 min, Wednesday: off, sick and Total: 75 min. Line 20
reads entry.minutes and line 23 reads entry.reason, and TypeScript
allows both, because on each line it knows which shape the card has.
entry.day works on both sides, as it did before the check.
A property that may be missing
Some chores are planned before they are done. On Thursday the card
says "windows", and the minutes get filled in later. Lesson 6.2 wrote
such a property with ?, as due?: string; here it is minutes?: number. Now a Chore may have minutes or not, and that changes
what the check can tell.
Bad — asks about the property a
Chore may lack, so a Chore can land in the else too.
Good — asks about the property
every DayOff has and no Chore has.
TypeScript refuses the first one on line 16: Property 'reason' does not exist on type 'Entry'., and the second line of the message names
Chore. Thursday's card has no minutes yet, so it fails the check
and reaches line 16 beside every day off. After the if, entry is
still an Entry, and a Chore has no reason. The second version
prints Thursday: windows, then Friday: off, trip. Here is what
each check leaves on its two sides:
"minutes" in entry"reason" in entryInside
Chore
DayOff
Else
Entry
Chore
With minutes optional, the else of the minutes check keeps
both shapes; the reason check splits them
Once entry is a Chore, its optional minutes is a number | undefined, as in lesson 6.2, and entry.minutes !== undefined is
allowed: every member left has that property. Even inside "minutes" in entry the value can still be undefined, because ? lets the
property hold it.
Predict before you run
On Sunday you watered the plants, which took no time worth counting,
so the card says minutes: 0. Lesson 7.3 lost a zero like this one
in a truthiness check. Decide what this program prints.
Predict
What does this program print?
The common mistake: checking !== undefined
Lessons 3.5 and 7.1 checked for a missing value with
!== undefined, so it is the check people reach for first. On a
union of shapes it does not work.
Common mistake
Checking a property that one member does not have
You want to add up the minutes only from the cards that have them:
TypeScript refuses line 18, and line 19 as well, before anything runs.
To compare entry.minutes with undefined, it has to read
entry.minutes first, and on an Entry that read is not allowed: a
DayOff has no such property, not even one that holds undefined.
Property 'minutes' does not exist on type 'Entry'.
The fix
Ask with in, which reads only the name:
Now it prints Total: 30 min. To spot this next time: a TS2339 (TS
for TypeScript, then the number of the message), Property '…' does not exist on type '…'., whose second line names a member of the
union, means that member has no such property at all. Any check that
reads entry.minutes on an Entry is refused the same way; ask about
the name with in.
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 "minutes" in entry ask?
Check yourself
Chore has minutes?: number. What is entry in the else of
"minutes" in entry?
Check yourself
Why is entry.minutes !== undefined refused on an Entry?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Narrowing with the in operator
What things are
Word
What it means
in
Asks whether an object has a property, by its name
A union of interfaces
Chore | DayOff: read only what both have
Code
Write
What you get
"minutes" in entry
true or false, while the program runs
if ("reason" in entry)
Inside: members with reason; else: the rest
Rules
Rule
What it means
The name goes in quotes
minutes in entry is TS2304
The name, not the value
{ minutes: 0 } passes the check
An optional property
Its member lands on both sides
Pick the property to ask
Every member on one side always has it
!== undefined
On a property one member lacks: TS2339
Where to go next
You can now tell objects of different shapes apart with in, say
which type TypeScript gives the value inside the check and in the
else, and pick a property that splits the union cleanly. The week
adds up, and a card with 0 minutes still counts as a chore.
Next is Discriminated unions. Finding a property that only one
shape has works for two shapes and gets harder with every new one.
The next lesson gives every shape the same property, holding a
different word, and TaskKit's commands become such a union.
Before you move on, copy the second week.ts, the one that adds up
the week, into VS Code (Visual Studio Code, your editor) and hover
over entry on line 20 and on line 23. Then add ? after minutes
on line 4, and TypeScript underlines two lines for two reasons. Line
20 gets lesson 3.3's TS18048, 'entry.minutes' is possibly 'undefined'.: in made sure the property is there, but lesson 6.2's
? also lets it hold undefined. Line 23 gets TS2339: hover over
entry, and it is an Entry now. Delete the ? once you have seen
both.
Exercise · Read the code
Which line inside the check is refused
entry is an Entry. Which line does TypeScript refuse?
Exercise · Predict the result
Which shapes a reason check keeps
A trip is a day off with a place. Which type does entry have on line
19?
cards.tsts
const monday = { day: "Monday", chore: "dishes", minutes: 30 };const wednesday = { day: "Wednesday", reason: "sick" };console.log("minutes" in monday);console.log("minutes" in wednesday);console.log("reason" in wednesday);