Skip to main content
⚑ OpenDevDocs
TypeScript
programmingtypescriptGuideBeginner

Literal Types and 'as const' in TypeScript

Mastering string, numeric, and boolean literal types in TypeScript: restricting variables to exact values, template literal types, and as const assertions.

Verified: 2026-09-30
Tested on:typescript 5.x
Platforms:
all
#typescript#literal-types#as-const#template-literals

Literal Types and 'as const' in TypeScript

A literal type is a more specific sub-type of a primitive type. Instead of allowing any string or number, a literal type restricts a variable to an exact value.


1. String and Number Literal Types

// Only these exact strings are valid:
type ButtonVariant = "primary" | "secondary" | "outline" | "ghost" | "danger";

type HttpSuccessCode = 200 | 201 | 204;

function setVariant(v: ButtonVariant) { /* ... */ }

setVariant("primary"); // βœ… Valid
// setVariant("custom");  // ❌ Error: Argument of type '"custom"' is not assignable to 'ButtonVariant'

2. The as const Assertion (Const Assertions)

When declaring objects or arrays, TypeScript widens property types to general primitives (string, number). Adding as const locks every property into its exact literal type and makes all properties readonly:

// Without 'as const': method is inferred as string (NOT "GET")
const config1 = {
  endpoint: "/api/users",
  method: "GET",
};

// With 'as const': method is locked to literal "GET"
const config2 = {
  endpoint: "/api/users",
  method: "GET",
} as const;

// config2.method is now type: "GET" (readonly)

3. Template Literal Types

Construct new string literal types by interpolating other types:

type Direction = "top" | "right" | "bottom" | "left";
type MarginClass = `m-${Direction}`; 
// Type: "m-top" | "m-right" | "m-bottom" | "m-left"

type EventName = `on${Capitalize<"click" | "change" | "submit">}`;
// Type: "onClick" | "onChange" | "onSubmit"

On this page