...

/

Readonly Collections and as const

Readonly Collections and as const

Learn how to freeze your arrays and tuples using readonly and as const to write safer, more predictable TypeScript.

JavaScript makes mutation easy—too easy. We can push to arrays, reassign fields, swap values, and delete keys—all without a second thought. And that’s fine… until our codebase grows and those silent mutations start turning into invisible bugs.

TypeScript gives us the power to prevent mutation by design. With a few small type-level tools, we can say:

“This array should never change.”
“This value should always stay the same.”
“This tuple should be locked down exactly as it is.”

And once we say it, TypeScript makes sure it stays that way.

In this lesson, we’ll learn how to:

  • Use readonly to make arrays and tuples immutable.

  • Use as const to freeze values and preserve their literal types.

  • Combine the two for precision, safety, and deep modeling power.

Let’s lock things down.

What makes immutability so important?

Mutable structures introduce risk. We might write a function that receives an array, modifies it, and causes unexpected side effects elsewhere. Or we might define a config value assuming it will stay fixed—only to see it get accidentally overwritten by another developer or refactor.

We need tools to signal intent: “Don’t touch this. Don’t change it. Trust that it will always look like this.”

Enter readonly and as const.

We’ve already seen how readonly locks object properties. Now let’s apply it to collections—arrays and tuples—and see how as const lets us freeze values and preserve their literal types.

Freezing arrays and tuples with readonly

Let’s start with the type-level tool: readonly.

This keyword creates read-only versions of arrays and tuples, preventing any mutation methods from being called. That means no push ...

Access this course and 1400+ top-rated courses and projects.