0Pricing
TypeScript Academy · Lesson

const Assertions with as const

Freeze objects and arrays into deeply readonly literal types.

const Assertions with as const is a free TypeScript Academy lesson on CoddyKit — lesson 3 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.

The as const Assertion

A const assertion, written as const, tells TypeScript to infer the most specific, deeply readonly type for a value. Object properties become literal and immutable; arrays become readonly tuples.

const point = { x: 10, y: 20 } as const;
// type: { readonly x: 10; readonly y: 20 }
console.log(point.x, point.y);

Without as const

Without the assertion, object properties widen. { x: 10 } infers x: number and is mutable. Compare this with the previous scene to see what as const changes.

const a = { x: 10 };       // x: number, mutable
const b = { x: 10 } as const; // x: 10, readonly
a.x = 99; // allowed
console.log(a.x, b.x);

Freezing Properties to Literals

as const turns every string property into its literal type. This is invaluable for config objects whose keys and values must stay exact.

const config = {
  env: 'production',
  retries: 3
} as const;
// env: 'production', retries: 3
console.log(config.env, config.retries);

readonly Prevents Mutation

Because as const makes properties readonly, the compiler blocks reassignment. The value is immutable at the type level, protecting it from accidental changes.

const settings = { mode: 'dark' } as const;
console.log(settings.mode);
// settings.mode = 'light'; // Error: readonly property

const Assertions on Arrays

Applied to an array, as const produces a readonly tuple of literal types. The length is fixed and each element keeps its exact value.

const colors = ['red', 'green', 'blue'] as const;
// type: readonly ['red', 'green', 'blue']
console.log(colors[0], colors.length);

No Mutation on Readonly Arrays

A readonly tuple rejects mutating methods like push or index assignment. The data is locked, which is exactly what you want for fixed lookup tables.

const days = ['Mon', 'Tue', 'Wed'] as const;
console.log(days.join(', '));
// days.push('Thu'); // Error: push does not exist on readonly tuple

Deriving a Union With typeof arr[number]

A powerful pattern: take a readonly array and derive a union of its element types using typeof arr[number]. You write the values once and get a literal union for free.

const ROLES = ['admin', 'editor', 'viewer'] as const;
type Role = typeof ROLES[number];
// 'admin' | 'editor' | 'viewer'
const r: Role = 'editor';
console.log(r);

Single Source of Truth

This keeps your runtime array and your type in sync. Add a value to the array, and the union updates automatically — no duplicated lists to maintain.

const SIZES = ['S', 'M', 'L', 'XL'] as const;
type Size = typeof SIZES[number];

function pick(s: Size): void {
  console.log('Picked', s);
}
pick('M');

const Assertions on Tuples

For tuples where element types differ, as const preserves both the exact types and the order, producing a precise readonly tuple.

const pair = [1, 'one'] as const;
// type: readonly [1, 'one']
console.log(pair[0], pair[1]);

Deep Readonly Behavior

as const applies deeply. Nested objects and arrays also become readonly with literal types, all the way down.

const data = {
  name: 'app',
  tags: ['a', 'b']
} as const;
// tags is readonly ['a', 'b']
console.log(data.name, data.tags[1]);

Const Assertions for Enums

A const object plus a derived union is a lightweight alternative to enum. You get literal values at runtime and an exact type, with zero extra emitted code.

const Status = {
  Idle: 'idle',
  Active: 'active'
} as const;
type Status = typeof Status[keyof typeof Status];
const s: Status = Status.Active;
console.log(s);

Quick Check

Test your understanding of const assertions.

Recap: as const

You learned that as const:

  • Infers the most specific, deeply readonly literal type.
  • Turns object properties into literal readonly fields.
  • Turns arrays into readonly tuples of literals.
  • Enables deriving unions via typeof arr[number], keeping values and types in sync.

Next, we combine literals into expressive unions.

const LEVELS = ['low', 'mid', 'high'] as const;
type Level = typeof LEVELS[number];
const chosen: Level = 'mid';
console.log(chosen);

Frequently asked questions

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

Yes — the full text of “const Assertions with 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 with as const”?

Freeze objects and arrays into deeply readonly literal types. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “const Assertions with 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. String and Numeric Literal Types
  2. Boolean Literals and Literal Inference
  3. const Assertions with as const
  4. Combining Literals into Unions
← Back to TypeScript Academy