![]() |
VOOZH | about |
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.
Here are the different types of literal types:
String literal types allow a variable to accept only a specific set of string values.
Output:
Up
Error: Type '"Forward"' is not assignable to type 'Direction'
In this example:
Numeric literal types restrict a variable to a specific set of numeric values..
Output :
4
Error: Type '7' is not assignable to type 'DiceRoll'In this example:
Boolean literal types constrain a variable to the boolean values true or false.
Output:
true
Error: Type 'false' is not assignable to type 'Success'
In this example: