Skip to content

mutating-getter ​

A getter that changes its object: it assigns a field, calls a method of the object that changes it, or lends the object or a part of it with mut(...). Reported at the getter. On by default, as an error.

Why: reading a property looks like reading. A caller that only borrowed the object (readCounter(c: Counter), no Mut) reads c.next and changes it, and nothing at the read says so: no-param-mutation follows methods into their bodies, but c.next isn't a call. Rust has no getters for this reason: what changes the object takes &mut self and is called as a method. Guide: State without globals.

✕ Rejectedmutating-getter
ts
export class Ticket {
  private issued = 0;

  get next(): number {
    this.issued += 1;
    return this.issued;
  }
}

✅ Accepted: a method changes the object, and callers that only borrow it are told

✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

export class Ticket {
  private issued = 0;

  next(): number {
    this.issued += 1;
    return this.issued;
  }

  get count(): number {
    return this.issued;
  }
}

export function issue(tickets: Mut<Ticket>): number {
  return tickets.next();
}

export function first(): number {
  const tickets = new Ticket();
  return issue(mut(tickets));
}
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

export class Ticket {
  private issued = 0;

  next(): number {
    this.issued += 1;
    return this.issued;
  }

  get count(): number {
    return this.issued;
  }
}

export function issue(tickets: Mut<Ticket>): number {
  return tickets.next();
}

export function first(): number {
  const tickets = new Ticket();
  return issue(tickets);
}

A getter that fills a memo on first read is fine when the memo is a field marked /** @cache */: changing it is no change of the object (Caches).

✓ Accepted0 diagnostics
ts
export class Catalog {
  readonly names: readonly string[];
  /** @cache */
  private readonly sorted = new Map<string, readonly string[]>();

  constructor(names: readonly string[]) {
    this.names = names;
  }

  get alphabetical(): readonly string[] {
    const known = this.sorted.get("a");
    if (known !== undefined) return known;
    const made = this.names.toSorted();
    this.sorted.set("a", made);
    return made;
  }
}

What the rule can't see ​

  • A getter of an object literal or of a class that isn't declared at the top level of a module: only module-level classes are read.
  • A change made deeper than the methods it follows: a getter that changes an element of a field (this.items[0].done = true) is reported, one that calls a method of an element (for (const item of this.items) item.close()) is followed only for the changing methods of arrays, maps and sets.
  • A getter declared in an interface or an object type: it has no body to read.

Related: no-param-mutation, which follows methods that change their object; guide: State without globals.

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