Enums: reading them in other people's code, and why modern code prefers literal unions
Read an enum in someone else’s code: a list of names, each standing for a value, like dish numbers on a café menu. Say which number or word each member holds, see why TypeScript accepts an enum while Node refuses to run it, and rewrite it as the union of words TaskKit already uses. Closes Module 7 with its one-page cheat sheet.
Fundamentals
TypeScript
Enums: reading them in other people's code, and why modern code prefers literal unions. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
A friend sends you the to-do app they wrote last year, and you open
its main file to see how they did statuses. You expect
"todo" | "done", like TaskKit's. Instead you find
status: Status.Done, and at the top of the file a block you have
never seen: enum Status { Todo, Done }. You copy the file into
your taskkit folder and check it: TypeScript finds nothing wrong.
You run it with node, and Node stops before the first line.
In a café, the screen over the counter says "Number 2 is ready". The
number means nothing until you look at the menu, where number 2 is
soup. An enum works like that menu, in code: a list of names, each
standing for a value. In this lesson you learn to read one: which
value each name holds, and how code checks it. You also find out why
Node refused your friend's file, and why new code, TaskKit included,
writes a union of literals instead, the "todo" | "done" of
lesson 7.2. This lesson calls it a union of words for short.
An enum: a name for each value
An enum, short for enumeration (a list of things, one by one),
is a set of named values written with the keyword enum. Each name
inside it is an enum member: one of the set, the way a member of a
union is one of its types. You read a member with a dot,
Status.Done, like a property of an object. The enum's name is a type
too: a box typed Status holds Status's members.
Here are TaskKit's two statuses beside the same two as an enum:
status.ts
type Status = "todo" | "done";let mine: Status;mine = "done";
Union of words
Does: The word itself is the value: "done"
Enum
Does: A name stands for the value: Status.Done
In a union the word is the value; in an enum a name stands
for it
In the enum, Todo = "todo" says which value the name stands for.
Your friend's enum wrote no values at all, so TypeScript gives each
member a number, in order, starting from 0, like the positions
in a list. Press Run:
It prints 0 1, then { title: 'Pay rent', status: 1 }. So when
your friend's app prints a task with status: 1, it means Done. A
member can also be given a number of its own, Low = 1, and then a
member written after it without = gets one more than the member
above it.
So enums come in two kinds. A number enum has members that hold
numbers, as above. A string enum writes a word for every member,
as in the Enum card above: Status.Done holds "done". Where code
does use an enum, the string kind is easier to read, because its
values say what they mean.
Checking an enum member
Code that uses an enum checks it the way you checked words in
lessons 7.3 and 7.5, with the member's name where the word was:
It prints [x] Pay rent and [ ] Call Mom. Each case narrows
task.status to one member, and as in lesson 7.5, the function needs
no return after the switch: the two cases cover both members.
An enum is a value, not only a type
Lesson 2.2 said that Node skips a type annotation when it runs the
file. It skips type Status = "todo" | "done"; the same way: a type
alias only tells TypeScript something. An enum does more. It also
makes an object that exists while the program runs; for a string enum,
one property per member. Press Run:
It prints { Todo: 'todo', Done: 'done' }, a real object. That is
what Status.Done reads from. A number enum's object also holds each
number with its name, so your friend's enum would print { '0': 'Todo', '1': 'Done', Todo: 0, Done: 1 }.
Node runs TypeScript in what it calls strip-only mode: it strips
the types out and does nothing else.
Press Next step to follow one file through both tools, the check
with tsc (the TypeScript compiler, which npx starts) and Node, and
switch between the two versions:
See it in your own terminal:
1
Make a file with an enum
In VS Code (Visual Studio Code, your editor), in ~/projects/taskkit,
make a new file called status.ts and type this into it:
2
Check it, then run it
The check prints nothing: TypeScript is happy. Node is not:
More lines follow: the at lines of lesson 1.5, a short block in
curly braces that repeats the error's code,
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX, and last your Node version, such
as Node.js v24.11.1. Read it the way that lesson taught. Two things
at the top are new. The first three lines are a place in Node's own
code where it gave up, so skip them. The lines marked > are the part
of your file Node could not run: the enum.
Your file's address, between them, is written with file:///, as you
saw in lesson 1.4. Then comes the sentence that says what went wrong,
one line in your terminal, broken in two here to fit the page.
3
Write it as a union, and run it again
Replace everything in status.ts with the same statuses as a union
of words:
status.ts was only for this test, so delete it: right-click it in
the panel on the left of VS Code and choose Delete. VS Code asks
whether you are sure; press Move to Trash on macOS and Linux, or
Move to Recycle Bin on Windows.
So why does Run on this page run enums? It hands your code to
TypeScript, which writes the JavaScript itself, the enum's object
included, and then runs that. Your friend's project does the same: a
command run before Node starts has TypeScript write a .js file, and
Node runs that. That is why their app works on their computer. node status.ts has no such step. In Module 9 you set up a project file
with a setting that makes the TypeScript check refuse an enum too, so
the check and Node agree.
Why modern code prefers a union of words
Node stopping is the biggest reason, and there are two more. Here are
the three ways to write Status, and what each does in three tests:
node status.tsstatus: "done"PrintsUnion of words
Runs
Fits
done
String enum
Stops
Error
The middle column is about writing the word itself. A union takes
"done" as you write it, and a word typed after node index.ts fits
too once a check like lesson 7.3's has narrowed it to "done". An
enum takes only its member, Status.Done: TypeScript refuses the
plain word with TS2322 (TS for TypeScript, then the number of the
message), as the common mistake below shows, and it refuses it even
after that check. The last column is what console.log shows when the
enum does run, as with Run on this page. A number enum prints 1,
and you need the enum to know what that means.
So the rule for this course is short. When you meet an enum, read
each member as the value behind it. When you write a set of words
yourself, write a union, as TaskKit's Status and the kind words
of its Command already are. To rewrite an enum as a union, list
its values joined with |, then write every Status.Done as the
value it stands for, "done".
Predict before you run
This enum gives its first member a number of its own. Decide what
the last line prints.
Predict
What does this program print?
The common mistake: a word for a string enum
A string enum's member holds a word, so writing the word itself
looks like it should work. In a union it does; in an enum it does
not.
Common mistake
Writing the word where a string enum is expected
You add a task to your friend's app, and you know Status.Done
holds "done":
TypeScript refuses line 10 before anything runs. A box typed with a
string enum takes the enum's members and nothing else, not even the
word a member holds.
Type '"done"' is not assignable to type 'Status'.
The fix
Write the member, by its name:
Now it prints done. When this error names an enum, look at the
top of the file for enum and write the member instead of the word.
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
Why does modern code write a union of words, not an enum?
Check yourself
Node refuses an enum, yet your friend's app with one works
on their computer. Why?
Check yourself
status is typed with enum Status { Todo = "todo", Done =
"done" }. What does status = "done" do?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Enums
What things are
Word
What it means
Enum
A set of named values, written with enum
Enum member
One name in the set: Status.Done
Number enum
Members holding numbers, from 0 unless one is written
String enum
A word written for every member
Code
Write
What you get
enum Status { Todo, Done }
Status.Todo is 0, Status.Done is 1
Low = 1, Normal
Normal is 2: one more than the member above
Todo = "todo"
Status.Todo holds "todo"
status: Status
A box for the enum's members
case Status.Done:
Narrows like a word does
Status.Done → "done"
Rewriting: each member becomes its value
Rules
Rule
What it means
node file.ts
Stops at an enum: types are skipped, values are not
A plain word
Refused where a string enum is expected: TS2322
Reading an enum
Read each member as the value behind it
Writing your own
A union of words: "todo" | "done"
Module 7 on one page
Cheat sheet
Module 7: Unions and Narrowing
7.1 Union types
Write
What you get
string | number
A string or a number
(string | number)[]
A list of strings and numbers
string | number[]
A string, or a list of numbers
7.2 Literal types and unions of literals ("todo" | "done")
Write
What you get
type Status = "todo" | "done";
A name for the two words
let mine: Status = "todo";
A box that takes only a status
"Done" for Status
Not "done": TS2820 suggests the member
7.3 Narrowing with typeof, equality, and truthiness
Write
What TypeScript knows
typeof x === "number"
Inside: number; else: the other members
status === "todo"
On "todo" | "done", the else is "done"
if (x)
Inside: no undefined; else: still the whole union
7.4 Narrowing with the in operator
Write
What you get
if ("reason" in entry)
Inside: members with reason; else: the rest
minutes in entry
The name goes in quotes: TS2304
7.5 Discriminated unions
Where to go next
You can now read an enum in someone else's code, say which value
each member holds, and rewrite it as a union of words that Node runs
as it is. That closes Module 7: TaskKit's statuses and commands are
unions, and you can tell their members apart on every branch.
Next is A catalogue of common TypeScript errors and how to fix
them, the first lesson of Module 8. You have met many error
codes on the way here, TS2322, TS2339 and TS2367 among them;
that lesson puts the common ones in one place, each with how to read
it and what to change.
Before you move on, open TaskKit's index.ts and find its two sets
of words: Status from lesson 7.2 and the kind words of Command
from lesson 7.5. For Status, write on paper what it would look
like as a string enum, and what tasks.push would have to say
instead of status: "todo".
Yourcode
TypeScript
Node
Screen
Your code: status.tsstatus.tsstatus.ts
TypeScript: checked ✓checked✓checked✓
Node: skips the types, runsskipsthetypes,runsskipsthetypes,runs
Screen: donedonedone
Time
The same file twice: Status as a union of words, or as an
enum. Its last line prints mine.
npx tsc --noEmit … reads every line and finds nothing wrong.
An enum is valid TypeScript.
Node skips type Status and : Status, and what is left is
plain JavaScript, so it runs.
The value of mine, printed: done.
TypeScript passes both; Node runs the union and stops at
the enum
All 4 steps shown.Step 1 of 4: Your code: status.tsStep 2 of 4: TypeScript: checked ✓Step 3 of 4: Node: skips the types, runsStep 4 of 4: Screen: done
All 4 steps shown.
Yourcode
TypeScript
Node
Screen
Your code: status.tsstatus.tsstatus.ts
TypeScript: checked ✓checked✓checked✓
Node: enum: not supportedenum:notsupportedenum:notsupported
The same file twice: Status as a union of words, or as an
enum. Its last line prints mine.
npx tsc --noEmit … reads every line and finds nothing wrong.
An enum is valid TypeScript.
Node only strips the types out, and an enum is code it would
have to write. It stops: TypeScript enum is not supported in strip-only mode.
Not one line of the program ran, though the check said yes.
TypeScript passes both; Node runs the union and stops at
the enum
All 4 steps shown.Step 1 of 4: Your code: status.tsStep 2 of 4: TypeScript: checked ✓Step 3 of 4: Node: enum: not supportedStep 4 of 4: Screen: nothing printed
All 4 steps shown.
done
Number enum
Stops
Error
1
Only the union runs in Node and takes a plain word; a number
enum prints a number
Write
What you get
kind: "chore"
A property only the word "chore" fits
switch (entry.kind)
In each case: the shape with that word
kind: string
entry stays the union: TS2339 inside the check
7.6 Enums: reading them in other people's code, and why modern code
prefers literal unions
Write
What you get
enum Status { Todo, Done }
Status.Todo is 0, Status.Done is 1
Todo = "todo"
Status.Todo holds "todo"
node file.ts
Stops at an enum: types are skipped, values are not
Exercise · Read the code
Which file does Node refuse
Each answer is the first line of a file, and TypeScript's check
passes all four. Which file does node file.ts refuse to run?
Exercise · Predict the result
Decode a number from the log
Your friend's app printed this task:
{ title: 'Fix the bike', priority: 2 }. Here is its enum. Which
member is the task's priority?
ts
enum Priority { Low, Normal, High, Urgent,}
Outputtext
done
status.ts
enum Status { Todo = "todo", Done = "done",}let mine: Status;mine = Status.Done;
friend.tsts
enum Status { Todo, Done,}const rent = { title: "Pay rent", status: Status.Done };console.log(Status.Todo, Status.Done);console.log(rent);
node:internal/modules/run_main:107 triggerUncaughtException( ^file:///Users/ana/projects/taskkit/status.ts:1 > enum Status { Todo = "todo", Done = "done", > } const mine: Status = Status.Done;SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]: TypeScript enum is notsupported in strip-only mode
node:internal/modules/run_main:107 triggerUncaughtException( ^file:///C:/Users/ana/projects/taskkit/status.ts:1 > enum Status { Todo = "todo", Done = "done", > } const mine: Status = Status.Done;SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]: TypeScript enum is notsupported in strip-only mode
node:internal/modules/run_main:107 triggerUncaughtException( ^file:///home/ana/projects/taskkit/status.ts:1 > enum Status { Todo = "todo", Done = "done", > } const mine: Status = Status.Done;SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]: TypeScript enum is notsupported in strip-only mode
Exercise · Fix the error
Rewrite the enum as a union
A colleague wrote this code, and Node refuses to run it. Rewrite
Status as a union of words, and replace every Status.Done and
Status.Todo with the word it stands for, so label("done") works.
Keep label's parameter typed Status. It should still print
finished and then still to do.