Course contentsShow
← TypeScript
Lesson 470 of 1,79611. Literal TypesFree lesson

as const on Arrays

Converting mutable arrays to readonly tuples with exact literal element types.

as const on Arrays

You'll learn how to lock down arrays into exact, readonly tuples with literal element types.

The Problem with Normal Arrays

When you write an array literal like ['red', 'green', 'blue'], TypeScript infers the type as string[] — a mutable array that can hold any number of strings. This means:

  • Element types are widened (literals become general primitives)
  • Array length isn't fixed
  • Elements can be modified or reassigned

Sometimes you want the exact opposite: a fixed list with precise literal values.

How as const Transforms Arrays

Adding as const to an array literal creates a readonly tuple with exact literal element types:

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

Now TypeScript knows:

  • Exactly 3 elements
  • First element is literally "red", not just string
  • The array cannot be modified (readonly)

Why This Matters

Think of it like shrink-wrapping: Without as const, your array is a flexible bag that can change shape and contents. With as const, it's vacuum-sealed — fixed length, exact values, tamper-proof.

const normalArray = [1, 2, 3];
// Type: number[]

const fixedTuple = [1, 2, 3] as const;
// Type: readonly [1, 2, 3]

// normalArray[0] = 99;  // ✓ Allowed
// fixedTuple[0] = 99;   // ✗ Error: readonly

Key Takeaway: Use as const on arrays when you need exact literal types and guaranteed immutability — perfect for configuration values, fixed option lists, or anywhere precision matters more than flexibility.