# Mutability Limits

> Enforcing immutability with readonly and 'as const'.

Subject: TypeScript | Lesson 35 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/mutability-limits
Previous: [Recursive Types](https://codewithanimation.in/typescript/recursive-types.md) | Next: [Partial & Required](https://codewithanimation.in/typescript/partial-and-required.md)

## Preventing Accidental Mutation

In JavaScript, `const` only prevents variable reassignment; it does not prevent you from mutating the properties of an object or pushing to an array.

TypeScript adds the `readonly` modifier and the `as const` assertion to enforce true deep immutability at compile-time.

## Literal Widening

If you declare `const method = "GET"`, TypeScript knows it's the literal type `"GET"`. But if you declare `const obj = { method: "GET" }`, TypeScript assumes you might change it later, so it widens the type to `string`.

If you pass that object to `fetch(url, { method: obj.method })`, TypeScript will complain that `string` is not assignable to `"GET" | "POST"`. Using `as const` prevents this widening.

## Syntax

```typescript
// 1. The readonly modifier (surface level)
type User = { readonly id: number; name: string };
const u: User = { id: 1, name: "Alice" };
u.id = 2; // Error: Cannot assign to 'id' because it is a read-only property.

// 2. The as const assertion (deep freeze)
const config = {
  endpoint: "/api",
  retries: 3
} as const;

config.retries = 4; // Error
// config is now exactly:
// { readonly endpoint: "/api"; readonly retries: 3; }
```

## Common mistake: Arrays vs ReadonlyArrays

A common pain point is passing a regular array into a function that expects a `readonly any[]` or vice-versa.

You can pass a mutable `string[]` into a function expecting a `readonly string[]` (it's safe). But you CANNOT pass a `readonly string[]` into a function expecting a `string[]` (the function might try to mutate it!).

## Tip: React State

When working with React, you should treat all state as immutable. Using `readonly` properties in your state types helps the compiler enforce good React habits.

## Try it: Try it

Toggle the `as const` assertion on the configuration object and watch how it restricts property mutation by turning the wide `string` type into a strict `readonly "GET"` literal.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/mutability-limits

## Check yourself

### Question 1

What is the difference between `const x = { a: 1 }` and `const y = { a: 1 } as const`?

- A. There is no difference; const already makes objects immutable.
- B. x.a is inferred as 'number', while y.a is inferred as the literal type '1' and is readonly.
- C. as const makes the object available globally.

Correct answer: B

Without as const, TS widens '1' to 'number' because you might mutate it. With as const, it locks it down to the exact literal value and applies readonly.

- Why not A: const only prevents reassignment of the variable itself, not mutation of its properties.
- Why not C: It only affects type inference.

## Remember this

- `const` prevents variable reassignment; it does NOT make objects immutable.
- `readonly` prevents a specific property from being mutated.
- `as const` freezes an entire object or array deeply, inferring literal types instead of wide types.
- Mutable arrays can be assigned to readonly arrays, but not vice-versa.
