ProgramGuru

String and Numeric Literal Types

Understand String and Numeric Literal Types

String and numeric literal types constrain a value to a small set of exact strings or numbers.

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

What you’ll learn

  • Explain how string and numeric literal types works and where its type rules apply.
  • Predict the exact output of the string and numeric literal types example.
  • Diagnose a common issue: Do not confuse numeric literals like 1 | 2 with a numeric range: the type does not automatically include numbers between arbitrary endpoints.

Build the mental model

Use literals for explicit modes, ratings, or protocol codes. A union such as 1 | 2 | 3 accepts exactly those numeric values, not an arbitrary number. Narrow literal types make incorrect call sites visible before execution.

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.

String and Numeric Literal Types: explained visually1 | 2 | 3 → Priority = 3 → Other numbers invalid1 | 2 | 3Priority = 3Other numbers invalidFollow the values through the operation
String and Numeric Literal Types: 1 | 2 | 3 → Priority = 3 → Other numbers invalid.

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 Priority = 1 | 2 | 3;
type Channel = "email" | "sms";
function format(priority: Priority, channel: Channel): string {
  return `P${priority} via ${channel}`;
}
console.log(format(1, "email"));
console.log(format(3, "sms"));

Verified output

P1 via email
P3 via sms

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

The first call combines numeric literal 1 with email. The second combines 3 with sms. Other numbers or strings fail a type check, but the generated code still handles plain JS values.

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 confuse numeric literals like 1 | 2 with a numeric range: the type does not automatically include numbers between arbitrary endpoints.

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 Speed type of 30 | 60 | 90 and print a label for each speed.

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

Which values are legal for Priority?
Choose one answer. You can try again.

Answer and explanation

Exactly 1, 2, or 3.

String and numeric literal types constrain a value to a small set of exact strings or numbers.

Review the key idea

String and Numeric Literal Types: String and numeric literal types constrain a value to a small set of exact strings or numbers.

The first call combines numeric literal 1 with email. The second combines 3 with sms. Other numbers or strings fail a type check, but the generated code still handles plain JS values.

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: Discriminated Unions →