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

Nested as const Inference

How as const recursively affects nested arrays and objects to preserve deep literal types.

Nested as const Inference

What you'll learn: How as const recursively locks down literal types at every level of nested structures, not just the surface.

The Power of Deep Literal Locking

When you apply as const to a complex structure containing nested arrays or objects, TypeScript doesn't just make the top level readonly and literal—it recursively applies the same treatment all the way down the tree.

This means:

  • Nested arrays become readonly tuples with literal element types
  • Nested objects have readonly properties with literal types
  • Every string, number, or boolean at any depth becomes its literal type
  • The entire structure becomes deeply immutable (at compile time)

How It Works in Practice

Without as const, nested structures lose precision:

const config = {
  server: {
    ports: [3000, 8080],
    mode: "production"
  }
};
// Type: { server: { ports: number[]; mode: string } }

With as const, everything locks down:

const config = {
  server: {
    ports: [3000, 8080],
    mode: "production"
  }
} as const;
// Type: {
//   readonly server: {
//     readonly ports: readonly [3000, 8080];
//     readonly mode: "production";
//   }
// }

Notice how ports became a tuple [3000, 8080] rather than number[], and mode became the literal "production" instead of string. The readonly modifier appears at every level.

Think of It Like Freezing Water

Applying as const is like flash-freezing a complex ice sculpture—it doesn't just freeze the outside; the coldness penetrates every layer, every cavity, every detail, preserving the exact shape at all depths.

Key Takeaway: as const applies recursively through all nested levels, converting every value to its literal type and marking every array and property as readonly, creating deeply immutable, precisely-typed structures.