TypeScript Literal Types Last Updated : 21 Jan, 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.Here are the types of literal types in TypeScript: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"; // 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 errormove = "Forward"; // Compile-time error2. 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 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 errorrollDice(); // Returning 7 causes a compile-time error3. 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: 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 erroroperation(); // Returning false causes a compile-time errorBest Practices for Using TypeScript Literal TypesUse 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. Comment More infoAdvertise with us Next Article TypeScript Literal Types P pranjalisingh1201 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript Interfaces Type TypeScript Interfaces Type offers an alternative method for defining an object's type, allowing for a distinct naming approach. Syntax:interface InterfaceName { property1: type1; property2?: type2; readonly property3: type3; // ... method1(): returnType1; method2(): returnType2; // ...}Parameters:in 2 min read TypeScript Literal Inference Type TypeScript Literal Inference Type allows you to create type annotations based on actual values, variables, or constants. This enhances type safety and code clarity by specifying that a variable can only hold specific, exact values. It's a way to express the precise values that a variable can take on 2 min read TypeScript Conditional Types In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Conditiona 4 min read TypeScript Aliases Type In TypeScript, a type alias allows you to assign a custom name to an existing type, enhancing code readability and reusability.Provide a shorthand for complex types like unions or objects.Allow naming of primitive types, object types, or functions for clarity.Simplify repetitive type definitions and 3 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read TypeScript any Type In TypeScript, any type is a dynamic type that can represent values of any data type. It allows for flexible typing but sacrifices type safety, as it lacks compile-time type checking, making it less recommended in strongly typed TypeScript code. It allows developers to specify types for variables, f 4 min read TypeScript Generic Types TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u 2 min read TypeScript Object Types TypeScript object types define the structure of objects by specifying property types, ensuring type safety and clarity when passing objects as function parameters.Optional properties, denoted with a ? provide flexibility for objects with varying properties. This approach enhances code robustness by 3 min read Opaque Types In TypeScript In TypeScript Opaque types concept allows for the definition of specialized types such as strings or numbers which are derived from primitive types but do not have the characteristics of the base types, the purpose of this is to prevent specific actions regarding the type in question, or to make thi 4 min read TypeScript Enums Type TypeScript Enums Type is not natively supported by Javascript, but it's a notable feature of the Typescript Programming language. In general, enumerations shortly called "enum" are available in most programming languages to create named consonants. Enums allow us to create a set of named constants, 5 min read Like