Literal Types in TypeScript
Welcome! This guide will explain Literal Types in TypeScript. They are a powerful way to make your code more predictable by narrowing down what values a variable can hold. Letβs walk through how they work.
What Are Literal Types?
Literal types let you specify an exact value for a variable, rather than just a general type like string or number. Think of it like saying: βThis variable can only be one specific thing.β
Example of Literal Types:
let direction: "up" | "down" | "left" | "right";
direction = "up"; // β
This worksdirection = "sideways"; // β Error: "sideways" is not assignableHere, direction can only have one of four literal values: "up", "down", "left", or "right".
Using Literal Types
Literal types work great for defining valid values more precisely. Letβs see where they can be useful.
1. String Literals
type Role = "admin" | "user" | "guest";
let myRole: Role = "admin"; // β
ValidmyRole = "superuser"; // β Error: "superuser" isn't part of the Role typeThis ensures that myRole will always have one of the allowed values.
2. Number Literals
let diceRoll: 1 | 2 | 3 | 4 | 5 | 6;
diceRoll = 3; // β
ValiddiceRoll = 7; // β Error: 7 is not allowedThis way, the value for diceRoll will always represent a valid dice roll.
Literal Types with Functions
You can use literal types with functions to ensure only certain inputs are accepted.
function respond(status: "success" | "error"): string { return `Response status: ${status}`;}
console.log(respond("success")); // β
Output: Response status: successconsole.log(respond("pending")); // β Error: "pending" is not a valid statusHere, the respond function only accepts "success" or "error" as arguments.
Combining Literals with Types
You can use literal types with other types to build more useful structures.
Example with Objects:
type Button = { label: string; color: "blue" | "green" | "red";};
const submitButton: Button = { label: "Submit", color: "blue",};
console.log(submitButton); // β
Output: { label: 'Submit', color: 'blue' }This ensures that the color of a button can only be "blue", "green", or "red".
Literal Types with const
When you declare a variable with const, TypeScript automatically assigns it a literal type. This helps keep things predictable.
const pi = 3.14; // pi's type is 3.14 (literal type), not just "number"
let radius: number = 10;let area = pi * radius * radius; // Works fineKey Points :
- Exact Values: Literal types allow only specific values for variables.
- Predictable Code: They help prevent invalid inputs and reduce bugs.
- Flexible: You can combine literal types with other types like objects or unions.
π‘ Conclusion
Literal types in TypeScript help you narrow down the range of valid values for variables and functions, making your code safer and more predictable. Use them whenever you need strict control over your data!
Happy coding with TypeScript! π