Literal Types
Use exact values as types in TypeScript.
Literal Types is a free TypeScript Academy lesson on CoddyKit — 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
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!

2
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"'.
3
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"'.
4
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'.
5
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"'.
6
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
7
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);
8
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!
9
type Switch = "on" | "off";
let state: Switch;
state = "on";
state = "off";
10
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!

Frequently asked questions
Is the “Literal Types ” lesson free?
Yes — the full text of “Literal Types ” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. 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.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- Arrays and Tuples
- Union and Intersection Types
- Enums
- Literal Types