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.
- All episodes are in the TypeScript from Zero playlist.
- Versions used: Node.js 26.13.0, npm 11.20.0, TypeScript 7.0.2.
Setup
We start in the terminal and open the project in VS Code:
$ touch src/literals.ts1. 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.
let dir: "up" | "down" = "up";
console.log(dir);$ node src/literals.ts2. Wrong value
What if we assign left? Node prints left and does not care.
dir = "left";
console.log(dir);$ node src/literals.ts3. 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.
$ npx tsc --noEmit4. Fix
We delete those two lines, and tsc is quiet.
$ npx tsc --noEmit5. 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.
const colors = ["red", "green"] as const;
console.log(colors);
type Color = (typeof colors)[number];
const c: Color = "red";
console.log(c);$ node src/literals.ts6. 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.
enum Level {
Low,
High,
}
console.log(Level.High);$ node src/literals.ts7. Replace
The modern replacement: a plain object with as const. It works everywhere, and Node runs it. Level dot high prints one.
const Level = {
Low: 0,
High: 1,
} as const;
console.log(Level.High);$ node src/literals.tsRecap
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.