TypeScript Literal Types
TypeScript's literal types allow developers to specify exact values for variables, function parameters, or properties, enhancing type safety by ensuring variables can only hold predefined values.
- Allow variables to have specific, exact values.
- Enhance code reliability by restricting permissible values.
Here are the types of literal types in TypeScript:
1. String Literal Types
String literal types allow a variable to accept only a specific set of string values.
type Direction = "Up" | "Down" | "Left" | "Right";
let move: Direction;
move = "Up"; // Valid assignment
// move = "Forward"; // Error: Type '"Forward"' is not assignable to type 'Direction'.
- The Direction type can only be one of the specified string literals: "Up", "Down", "Left", or "Right".
- Assigning any value outside this set results in a compile-time error.
Output:
move = "Up"; // No error
move = "Forward"; // Compile-time error
2. Numeric Literal Types
Numeric literal types restrict a variable to a specific set of numeric values..
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
function rollDice(): DiceRoll {
return 4; // Valid return value
// return 7; // Error: Type '7' is not assignable to type 'DiceRoll'.
}
- The DiceRoll type allows only the numbers 1 through 6.
- Returning a number outside this range causes a compile-time error.
Output :
rollDice(); // Returns 4 without error
rollDice(); // Returning 7 causes a compile-time error
3. Boolean Literal Types
Boolean literal types constrain a variable to the boolean values true or false.
type Success = true;
function operation(): Success {
return true; // Valid return value
// return false; // Error: Type 'false' is not assignable to type 'true'.
}
- The Success type is strictly true.
- Returning false would result in a compile-time error.
Output:
operation(); // Returns true without error
operation(); // Returning false causes a compile-time error
Best Practices for Using TypeScript Literal Types
- Use Literal Types for Exact Values: Define variables with literal types to restrict them to specific, predetermined values, enhancing code predictability.
- Combine with Union Types: Utilize union types to allow variables to accept a finite set of literal values, improving type safety.
- Leverage Type Aliases: Create type aliases for complex literal type combinations to simplify code and enhance readability.
TypeScript Literal Types - FAQs
What are literal types in TypeScript?
Literal types in TypeScript allow variables to be assigned only specific values, providing more precise type definitions.
How do literal types differ from regular types?
Regular types like string or number allow any value of that type, whereas literal types restrict variables to exact values, such as "success" or 42.
Can I combine multiple literal types?
Yes, by using union types (e.g., "start" | "stop"), you can allow a variable to accept multiple specific values.
How do literal types enhance code safety?
Literal types enable the compiler to catch invalid assignments at compile time, reducing runtime errors and improving code reliability.
Are literal types applicable to function parameters?
Yes, you can define function parameters with literal types to ensure only specific arguments are passed, enforcing stricter type checks.