unneeded-mut
A parameter declared Mut<T> that the function never changes, and a loan mut(x) at a parameter that isn't Mut. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.
Why: Mut<T> tells the caller "this function changes what you pass it". When it doesn't, the signature claims more than the function does: a reader looks for a change that isn't there, and code that only lends its data can't call it without owning that data. Rust warns the same way (unused_mut). Borrow only what a function changes. Guide: State without globals.
✕ Rejectedunneeded-mut
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
type TodoList = { readonly done: number; readonly items: string[] };
export function summary(list: Mut<TodoList>): string {
return `${list.done} of ${list.items.length} done`;
}✅ Accepted: it only reads, so it borrows
✓ Accepted0 diagnostics
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
type TodoList = { readonly done: number; readonly items: string[] };
export function summary(list: TodoList): string {
return `${list.done} of ${list.items.length} done`;
}
// A `Mut<TodoList>` is a `TodoList` wherever one is read: passed as it is, not lent.
export function add(list: Mut<TodoList>, item: string): string {
list.items.push(item);
return summary(list);
}The quick fix Take it borrowed removes the Mut<…> (list: Mut<TodoList> becomes list: TodoList), and the import when nothing else uses it. Callers keep type-checking: their mut(list) still fits a TodoList, and is then reported as below.
A loan where nothing is lent
mut(x) at a parameter (or a component's prop) that isn't Mut is reported too: the call doesn't change x, and a loan written where nothing is changed hides the calls that do. The quick fix Pass it as it is: remove mut(…) takes the loan away.
✕ Rejectedunneeded-mut
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type TodoList = { readonly done: number; readonly items: string[] };
function summary(list: TodoList): string {
return `${list.done} of ${list.items.length} done`;
}
export function add(list: Mut<TodoList>, item: string): string {
list.items.push(item);
return summary(mut(list));
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type TodoList = { readonly done: number; readonly items: string[] };
function summary(list: TodoList): string {
return `${list.done} of ${list.items.length} done`;
}
export function add(list: Mut<TodoList>, item: string): string {
list.items.push(item);
return summary(list);
}Only where the checker knows what the call runs, as for missing-mut; a parameter past the last one counts when the function has no rest parameter.
What counts as a change
- an assignment,
++,deleteor a method that changes its receiver, on the parameter or anything reached through it (list.items.push(...)), also through a local (const items = list.items), a loop variable or a callback's parameter (list.tasks.forEach((t) => (t.done = true))); - lending it, or a part of it, to a function whose parameter is
Mut<...>(save(mut(list)), like&mut listin Rust). Fix that function first; then this one is reported too; - passing it to a function of yours that changes it without saying so: one that passes it on to a
Mut<...>parameter, or calls a method on it the checker doesn't know. Functions in other files are followed.
A method counts as a change unless it's known to only read: map, filter, find, some, includes, get, has, slice, join, ... A function of yours that only reads the value or hands a part of it back (openTasks(list)) is fine, as long as what it hands back is only read too.
Readonly<Mut<T>> is a loan like Mut<T>: TypeScript keeps the brand, so callers still write mut(x), and Readonly stops assignments to its fields only (a method that changes it still can). Where the function only reads it, it is reported like Mut<T>: take T (or Readonly<T>).
Not reported
Where the checker can't tell, it doesn't report:
- the parameter, or a part of it, is returned, stored (
lists.push(list),{ list }), thrown, or passed to a package, a callback parameter (run(list)) or a method of an unknown object. Those may change it later. (A closure that uses it is followed where it's written: its uses count like any other.) - the signature is fixed from outside: a function written as an argument (
handlers.push((list: Mut<TodoList>) => ...)), a function assigned to a typed variable, a method of a class thatimplementsorextendssomething, a method of an object literal that has a type.
✅ Accepted: the type says what a handler gets, whether or not this one changes it
✓ Accepted0 diagnostics
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
type TodoList = { readonly done: number };
type Plugin = { readonly update: (list: Mut<TodoList>) => void };
export const logger: Plugin = {
update(list: Mut<TodoList>): void {
console.log(list.done);
},
};