Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A literal type in TypeScript is a type that represents one exact, specific primitive value rather than a broader category of values. By specifying a literal type, the compiler restricts a variable to hold only that exact value, effectively narrowing the domain of a collective primitive type (such as string, number, or boolean) to a single instance. A literal type is a strict subtype of its corresponding primitive type.

Core Literal Types

TypeScript supports literal types for three primary primitives: strings, numbers, and booleans.
// String Literal Type
let exactString: "Hello";
exactString = "Hello"; // Valid
exactString = "World"; // Type Error: Type '"World"' is not assignable to type '"Hello"'.

// Numeric Literal Type
let exactNumber: 42;
exactNumber = 42; // Valid
exactNumber = 50; // Type Error: Type '50' is not assignable to type '42'.

// Boolean Literal Type
let exactBoolean: true;
exactBoolean = true;  // Valid
exactBoolean = false; // Type Error: Type 'false' is not assignable to type 'true'.

Type Inference and Widening

TypeScript’s type inference engine handles literal types differently depending on how a variable is declared. This behavior is governed by a concept called literal widening. When a variable is declared with let or var, the compiler assumes the value may change. Therefore, it widens the inferred type from the literal value to the underlying primitive type. When declared with const, the compiler guarantees the value cannot be reassigned, so it infers the exact literal type.
// Widened Inference
let widenedString = "TypeScript"; // Inferred type: string
let widenedNumber = 100;          // Inferred type: number

// Narrowed (Literal) Inference
const narrowedString = "TypeScript"; // Inferred type: "TypeScript"
const narrowedNumber = 100;          // Inferred type: 100

Template Literal Types

TypeScript allows the construction of new string literal types by interpolating existing string literal types using template literal syntax at the type level. When combined with unions, template literal types distribute over the union members to generate a cross-product of exact string literals.
type Prefix = "api" | "web";
type Action = "read" | "write";

// Distributes to: "api_read" | "api_write" | "web_read" | "web_write"
type Endpoint = `${Prefix}_${Action}`; 

The as const Assertion (Const Assertions)

To prevent literal widening in complex data structures like objects or arrays, TypeScript provides the as const assertion. Applying as const instructs the compiler to:
  1. Infer exact literal types for all properties or elements.
  2. Recursively apply the readonly modifier to all properties.
  3. Treat arrays as readonly tuples rather than mutable arrays of a widened type.
// Without 'as const'
const defaultObject = {
  protocol: "https",
  port: 443
}; 
// Inferred type: { protocol: string; port: number; }

// With 'as const'
const strictObject = {
  protocol: "https",
  port: 443
} as const; 
// Inferred type: { readonly protocol: "https"; readonly port: 443; }

// Array with 'as const'
const strictArray = [1, 2, 3] as const;
// Inferred type: readonly [1, 2, 3]
Master TypeScript with Deep Grasping Methodology!Learn More