ProgramGuru

Literal Types

Understand Literal Types

A literal type accepts a particular value rather than every value of a primitive kind. For example, "ready" is narrower than string.

What you will learn: the purpose of literal types, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how literal types works and where its type rules apply.
  • Predict the exact output of the literal types example.
  • Diagnose a common issue: Do not assume a literal type validates a value received from JSON, user input, or an external API at runtime.

Build the mental model

Literal unions model finite sets of valid states. A const declaration often preserves literal inference; a let variable may widen to string or number because it can later be reassigned. Literal constraints disappear when JavaScript runs.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Literal Types: explained visuallyStatus union → "ready" allowed → "unknown" rejectedStatus union"ready" allowed"unknown" rejectedFollow the values through the operation
Literal Types: Status union → "ready" allowed → "unknown" rejected.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

type Status = "idle" | "ready" | "done";
function describe(status: Status): string {
  return `Status: ${status}`;
}
const current: Status = "ready";
console.log(describe(current));
console.log(describe("done"));

Verified output

Status: ready
Status: done

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

Status permits only three string values in checked source. Both calls provide valid members; interpolation inserts each actual runtime string into the message.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not assume a literal type validates a value received from JSON, user input, or an external API at runtime.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Create a Direction type with "north", "south", "east", and "west" and test a function that accepts it.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What does a string literal union protect at compile time?
Choose one answer. You can try again.

Answer and explanation

It rejects values outside the declared set when the checker knows the value.

A literal type accepts a particular value rather than every value of a primitive kind. For example, "ready" is narrower than string.

Review the key idea

Literal Types: A literal type accepts a particular value rather than every value of a primitive kind.

Status permits only three string values in checked source. Both calls provide valid members; interpolation inserts each actual runtime string into the message.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: String and Numeric Literal Types →