M Logo
๐Ÿ“ TypeScript from Zero ยท Part 18 of 50

TypeScript Literal Types, as const and Why Node Rejects enum

November 6, 2026

Welcome to episode 18 of TypeScript from Zero, a series for complete beginners (no JavaScript needed). Welcome back to TypeScript from zero! Today: types that are exact values. We stay in the shop project, with a new file called literals.ts. Every code block below is the code from the video, ready to copy; run each file to see the output.

Setup

We start in the terminal and open the project in VS Code:

bash
$ touch src/literals.ts

1. Literal

A type does not have to be a whole group of values. It can be one exact value. Here the variable dir may only be the text up, or the text down. A union of exact values, and nothing else is allowed.

ts
let dir: "up" | "down" = "up";
console.log(dir);
bash
$ node src/literals.ts

2. Wrong value

What if we assign left? Node prints left and does not care.

ts
dir = "left";
console.log(dir);
bash
$ node src/literals.ts

3. tsc

tsc does care: the type left is not assignable to up or down. TS2322. Exact value types are a great way to list the allowed options.

bash
$ npx tsc --noEmit

4. Fix

We delete those two lines, and tsc is quiet.

bash
$ npx tsc --noEmit

5. as const

As const freezes a value into its exact type. The array of colors is no longer a list of any strings, it is exactly red and green, read only. And with type of and square brackets with number, we turn the list into a union type: red or green.

ts
const colors = ["red", "green"] as const;
console.log(colors);
type Color = (typeof colors)[number];
const c: Color = "red";
console.log(c);
bash
$ node src/literals.ts

6. enum

Many languages have an enum, and TypeScript has one too. tsc is fine with it. But when we run the file, Node refuses: TypeScript enum is not supported in strip only mode. An enum is not just types, it creates code, and Node only removes types.

ts
enum Level {
  Low,
  High,
}
console.log(Level.High);
bash
$ node src/literals.ts

7. Replace

The modern replacement: a plain object with as const. It works everywhere, and Node runs it. Level dot high prints one.

ts
const Level = {
  Low: 0,
  High: 1,
} as const;
console.log(Level.High);
bash
$ node src/literals.ts

Recap

Today: literal types, a union of exact values, as const, the type from a constant array, and why we use an object with as const instead of enum. Next episode: discriminated unions.

ยฉ 2026 All Rights Reserved By Mahmudul Alam