TypeScript Literal Inference Type Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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, ensuring that your code is more specific and less error-prone. Syntax:const variableName = value as Type;Parameters:variableName: The name of the variable you want to create or update.value: The specific value or literal you want to infer the type from.Type: The type you want to assign based on the value.Example 1: In this example, TypeScript's type inference is used to create a string literal type, "red," by defining a constant variable named color with the value "red" and creating a corresponding type, ColorType, based on the typeof operator. JavaScript // Define a variable 'color' and set it // to the string literal "red" as a constant. const color = "red" as const; // Creating a type 'ColorType' by using // 'typeof' on the 'color' variable. type ColorType = typeof color; // Printing the results to the console. console.log(color); Output: redExample 2: In this example, we have defined a custom Status type allowing "active" or "inactive" values. The user object with username and status properties ensures only valid values are assigned to status, improving type safety and code clarity. The console.log statements correctly display the properties with the specified type constraints. JavaScript // Defining a custom type `Status` that // can only have the values "active" or "inactive." type Status = "active" | "inactive"; // Creating an object `user` with // properties `username` and `status`. const user = { // The `username` property is a string. username: "GFG1", // The `status` property is inferred as a string // literal "active" due to the `as Status` assertion. status: "active" as Status, }; console.log(`Username: ${user.username}`); console.log(`Status: ${user.status}`); Output: Username: GFG1Status: active Comment More infoAdvertise with us Next Article TypeScript Literal Inference Type A anjalibo6rb0 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript Inference with Template Literals TypeScript Inference with Template Literals helps to create specific types based on string patterns. However, they're not mainly used for making sure that an attribute's type matches its callback function's argument type. TypeScript uses different methods like inference and generics for that job. Te 2 min read TypeScript Inference TypeScript's type inference automatically determines the types of variables, function return values, objects, and arrays based on their assigned values and usage.This feature reduces the need for explicit type annotations, simplifying code while maintaining type safety.By analyzing the context and i 2 min read What are string literal types in TypeScript ? The string literal type was added in TypeScript version 1.8. String literal types work well with union types and type aliases in practice. These properties can be combined to give strings enum-like functionality. The string literal type allows you to specify a set of possible string values for a var 3 min read TypeScript - Type Annotations and Type Inference TypeScript is a superset of JavaScript that adds static typing, helping developers catch errors early. Two core features are type annotations- where developers explicitly define variable, parameter, and return types, and type inference, where TypeScript automatically deduces types based on values or 5 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 Like