TypeScript Literal Types Last Updated : 09 Sep, 2025 Comments Improve Suggest changes Like Article Like Report 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.Types of literal types Here are the different types of literal types:1. String Literal TypesString literal types allow a variable to accept only a specific set of string values. JavaScript type Direction = "Up" | "Down" | "Left" | "Right"; let move: Direction; move = "Up"; // move = "Forward"; Output:Up Error: Type '"Forward"' is not assignable to type 'Direction' In this example: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.2. Numeric Literal TypesNumeric literal types restrict a variable to a specific set of numeric values.. JavaScript type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6; function rollDice(): DiceRoll { return 4; // Valid // return 7; Error } console.log(rollDice()); Output :4 Error: Type '7' is not assignable to type 'DiceRoll'In this example:The DiceRoll type allows only the numbers 1 through 6.Returning a number outside this range causes a compile-time error.3. Boolean Literal TypesBoolean literal types constrain a variable to the boolean values true or false. JavaScript type Success = true; function operation(): Success { return true; // Valid return value // return false; // Error } console.log(operation()); Output:trueError: Type 'false' is not assignable to type 'Success'In this example:The Success type is strictly true.Returning false would result in a compile-time error. Comment P pranjalisingh1201 Follow Improve P pranjalisingh1201 Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like