TypeScript Academy · Lesson

Literal Types

Use exact values as types in TypeScript.

Lesson 4 of 410 steps

Literal Types is a free TypeScript Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.

Literal Types

Welcome to the next lesson! In this lesson, you’ll learn about literal types, a TypeScript feature that allows you to define exact, specific values for variables. This makes your code more predictable and type-safe. Let’s dive in!

Literal Types  — illustration 1

What Are Literal Types?

Literal types in TypeScript allow a variable to hold a specific value, rather than just a general type like string or number. For example, you can restrict a variable to only hold the value "yes" or "no".

Example:

Task: Declare a variable with a literal type and assign valid and invalid values to it.

let answer: "yes" | "no";
answer = "yes"; 
// Valid
// answer = "maybe"; 
// Error: Type '"maybe"' is not assignable to type '"yes" | "no"'.

Literal Types with Strings

String literal types restrict a variable to a specific set of string values. Example:

Task: Create a type for directions and assign values to a variable.

type Direction = "up" | "down" | "left" | "right";

let move: Direction;
move = "up"; // Valid
// move = "forward"; 
// Error: Type '"forward"' is not assignable to type '"up" | "down" | "left" | "right"'.

Literal Types with Numbers

You can also use literal types with numbers to restrict a variable to specific numeric values. Example:

Task: Define a numeric literal type for dice rolls and test it.

type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;

let roll: DiceRoll;
roll = 4; // Valid
// roll = 7; 
// Error: Type '7' is not assignable to type '1 | 2 | 3 | 4 | 5 | 6'.

Literal Types in Functions

You can use literal types in function parameters to allow only specific values. Example:

Task: Write a function with a parameter that uses literal types.

function setStatus(status: "active" | "inactive"): void {
  console.log(`The status is now ${status}`);
}

setStatus("active"); 
// Output: The status is now active
// setStatus("pending"); 
// Error: Type '"pending"' is not assignable to type '"active" | "inactive"'.

Literal Types and Type Aliases

To reuse literal types, you can define them as type aliases. Example:

Task: Create a type alias for traffic light colors and use it in a variable.

type TrafficLight = "red" | "yellow" | "green";

let light: TrafficLight = "red";
console.log(light);
 // Output: red

Using Literal Types with Objects

You can use literal types inside objects to restrict the values of specific properties. Example:

Task: Create an object type with a property that uses literal types.

type Button = {
  label: string;
  size: "small" | "medium" | "large";
};

let submitButton: Button = {
  label: "Submit",
  size: "medium",
};

console.log(submitButton);

Common Mistakes

Here are some common mistakes when using literal types:

  • Forgetting to update all allowed values when modifying a literal type.
  • Assigning values not included in the literal type (TypeScript will catch this).
  • Using literal types for highly dynamic data, where flexibility is more important.

Tip: Use literal types for controlled, predictable scenarios!

type Switch = "on" | "off";

let state: Switch;
state = "on";
state = "off";

Great Job!

Congratulations! You’ve learned how to use literal types in TypeScript to define precise and restricted values. Literal types are especially useful for creating predictable and type-safe code. In the next lesson, we’ll explore TypeScript’s support for working with functions. Let’s keep coding!

Literal Types  — illustration 10
Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “Literal Types ” lesson free?

Yes — the full text of “Literal Types ” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 4 lessons in total.

What will I learn in “Literal Types ”?

Use exact values as types in TypeScript. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Literal Types ” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays and Tuples
  2. Union and Intersection Types
  3. Enums
  4. Literal Types
← Back to TypeScript Academy