Skip to content

readonly-dropped ​

A value whose written type has a readonly property is given a place whose written type has the same property writable: an annotated const or let, a declared return type, a field (also its initialiser in a class body) or a property, an element of a list, a value put into a Map or a Set. On by default, as an error. Quick fix, for a frozen value under an annotation: Keep it read-only: Readonly<…>.

Why: TypeScript checks readonly on a property in neither direction, so one annotation takes it off: const rw: Loose = frozen compiles, and so does rw.retries = 5 after it. The write then changes what the value's type says can't change. A frozen object throws (TypeError: Cannot assign to read only property 'retries', in a module); a shared one changes under every other holder that trusted the readonly. Mut<T> refuses this move at a call (a value with a readonly property can't be lent where the function may write it, State without globals), and covariant-alias refuses it for a container's element type; an annotated binding is the way around both.

❌ Rejected: every caller gets a writable view of a frozen object (defaults().retries = 5 throws)

✕ Rejectedreadonly-dropped
ts
type Limits = { retries: number };

const DEFAULTS: Readonly<Limits> = Object.freeze({ retries: 3 });

export function defaults(): Limits {
  return DEFAULTS;
}

✅ Accepted: keep it read-only on this side too, or give the place a copy it may change

✓ Accepted0 diagnostics
ts
type Limits = { retries: number };

const DEFAULTS: Readonly<Limits> = Object.freeze({ retries: 3 });

export function defaults(): Readonly<Limits> {
  return DEFAULTS;
}

export function tuned(): Limits {
  const limits: Limits = { ...DEFAULTS };
  limits.retries = 5;
  return limits;
}

❌ Rejected: a frozen table typed writable, and a getter's value handed out as a writable field

✕ Rejectedreadonly-dropped
ts
interface Palette {
  ink: string;
  paper: string;
}

export const PALETTE: Palette = Object.freeze({ ink: "#202020", paper: "#fafaf0" });

class Session {
  constructor(readonly user: string) {}
}

type Draft = { user: string };

export function draftOf(session: Session): Draft {
  return session;
}
✓ Accepted0 diagnostics
ts
interface Palette {
  readonly ink: string;
  readonly paper: string;
}

export const PALETTE: Palette = Object.freeze({ ink: "#202020", paper: "#fafaf0" });

class Session {
  constructor(readonly user: string) {}
}

type Draft = { user: string };

export function draftOf(session: Session): Draft {
  return { user: session.user };
}

What counts as readonly, read from the types the program declares, in the file or imported: a readonly property of a type literal, an alias or an interface (and of what an interface extends), Readonly<T> (also as a parameter's type), a class's readonly field or readonly constructor parameter, and a getter without a setter. A value's type is its annotation (a local's, a parameter's, a field's, a member of one), the declared return type of a module-level function it is the call of, the type of a module-level const (here or imported), Object.freeze(...) (every property of what it freezes), or { ... } as const; a const without an annotation has the type of what it was given (new Settings(3) is a Settings, a class of the program; new Map<string, Loose>() too).

A module-level constant written through is also no-mutable-global's: that rule reports the write (PALETTE.ink = ... in a function); this one reports the annotation that makes the write compile, wherever the value goes, before anything writes it. One level down counts too: an { readonly inner: { readonly retries: number } } given to { inner: { retries: number } } can write inner.retries.

Passing the value to a parameter that isn't Mut is a read (a function may not write what it borrows: no-param-mutation), and stays silent. A readonly T[] given to a T[] is refused by TypeScript itself, and a Frozen[] given to a Loose[] is covariant-alias's.

What the rule can't see ​

  • A type the program doesn't declare as an object: a package's type, a type computed from others (Pick, a mapped type, a conditional), an index signature, a union of object types.
  • A value whose type isn't written: what a method returns, what TypeScript infers through a generic, an async function's result, a value put into a collection whose element type is inferred (new Map() filled later).
  • Two levels down or more (a.b.c), and a property whose value is a union of object types.
  • A fresh object a function returns read-only (function defaults(): Readonly<Options> returning a new literal each call) and a caller that owns it and annotates it writable: it is reported, though nothing else holds the object. The return type is the function's promise; a caller that changes its own copy writes { ...defaults() }.

Related: covariant-alias, no-param-mutation; guide: State without globals.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra