0Pricing
TypeScript Academy · Lesson

const Assertions and as const

Use as const to infer literal and tuple types precisely.

const Assertions and as const is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is as const?

as const is a type assertion that tells TypeScript to infer the most specific literal types possible for an expression, and to make all properties readonly.

const colors = ["red", "green", "blue"] as const;
// type: readonly ["red", "green", "blue"]

Preventing Widening with as const

Without as const, string values in arrays and objects widen to string. With it, they stay as their literal types.

const dir1 = { direction: "left" };
// dir1.direction: string

const dir2 = { direction: "left" } as const;
// dir2.direction: "left"

as const on Object Literals

Every property of an object with as const becomes readonly and keeps its literal type recursively.

const theme = {
  color: "dark",
  sizes: { sm: 8, md: 16 },
} as const;
// theme.color: "dark", theme.sizes.sm: 8 (all readonly)

as const for Tuple Types

Arrays with as const become readonly tuples with literal element types — useful for defining fixed argument lists.

const args = [1, "hello", true] as const;
// type: readonly [1, "hello", true]

Deriving Union Types from as const

A common pattern: define an as const array of values, then derive a union type from it using typeof and indexed access.

const STATUSES = ["pending", "active", "done"] as const;
type Status = typeof STATUSES[number];
// type Status = "pending" | "active" | "done"

as const in Function Arguments

You can use as const inline when calling functions to preserve literal types through the call boundary.

function setMode(mode: "dark" | "light") {}
setMode("dark" as const); // safe, preserves literal

Readonly Recursion

as const applies Readonly recursively. Nested objects and arrays all become readonly, preventing deep mutations.

const config = { db: { host: "localhost", port: 5432 } } as const;
// config.db.port = 5433; // Error: readonly

as const vs Object.freeze

as const is a compile-time assertion — no runtime protection. Object.freeze is runtime. They serve complementary roles.

const obj = Object.freeze({ x: 1 }); // runtime immutability
const obj2 = { x: 1 } as const;      // compile-time literal types

Const Assertions vs Type Annotations

A type annotation widens: const x: string = "hi" gives type string. as const narrows: const x = "hi" as const gives "hi".

const a: string = "hi"; // type: string
const b = "hi" as const; // type: "hi"

Practical Use: Enum-Like Objects

Many codebases use as const objects as an alternative to enums, avoiding enum pitfalls while retaining type safety.

const Direction = { Up: "UP", Down: "DOWN" } as const;
type Direction = typeof Direction[keyof typeof Direction];
// "UP" | "DOWN"

Recap: as const

as const makes TypeScript infer literal types and treat all properties as readonly. It is essential for deriving precise union types from arrays and building safe config objects.

Quick Check

What does as const do to an array?

What You Learned

as const is a powerful tool for locking in literal types, creating readonly structures, and deriving precise union types from arrays. It is a cornerstone of advanced TypeScript patterns.

Frequently asked questions

Is the “const Assertions and as const” lesson free?

Yes — the full text of “const Assertions and as const” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “const Assertions and as const”?

Use as const to infer literal and tuple types precisely. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “const Assertions and as const” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Type Widening and Narrowing Mechanics
  2. Contextual Typing: Inference from Context
  3. Freshness and Excess Property Checking
  4. const Assertions and as const
← Back to TypeScript Academy