0Pricing
TypeScript Academy · Lesson

const Assertions with as const

Freeze objects and arrays into deeply readonly literal types.

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);

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