Skip to content
Readonly

    ↑↓ move · ⏎ open · esc close

    Object shapes

    Readonly

    Adds `readonly` to every property of an object, one level deep.

    What it is

    The standard homomorphic mapped type for a read-only view. It walks `keyof T`, adds the modifier to every property, and preserves the rest of the shape. The word “view” matters: the generated JavaScript contains no freeze and a mutable alias may still change the same object.

    Examples

    • Readonly<{ a: string; b: number }>
      { readonly a: string; readonly b: number; }
    • Readonly<{ nested: { value: number } }>
      { readonly nested: { value: number; }; }

      The outer reference is read-only; `nested.value` is not.

    • Readonly<[1, 2]>
      readonly [1, 2]

    Each resolved type above was printed by TypeScript 6.0.3, not written by hand.

    What it does not do

    • It is not deep. Nested objects keep their original modifiers, so a read-only property can still point at mutable data.
    • It does not call `Object.freeze` or survive at run time. It restricts assignments the compiler can see, not mutations performed elsewhere.

    Takeaway

    `Readonly<T>` is a compile-time read-only view of one level. Use recursion for depth and a run-time operation for actual freezing.