missing-mut
A value passed to a parameter declared Mut<...> (of a function, a method, a callback type, a component's prop) without mut(...) at the call. On by default, as an error.
Why: Mut<T> is SAFE-TS's &mut T: the function may change what it is given. The caller says so too, by writing mut(value) where it lends it, like Rust's &mut value, so a call shows which of its arguments it changes. Mut<T> is a type of its own, and TypeScript refuses the call without the loan; this rule says the same in the editor and the lint integrations, which don't run TypeScript, and offers the loan as a quick fix. Guide: State without globals.
✕ Rejectedmissing-mut
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
type Cart = { readonly id: string; items: string[] };
function addItem(cart: Mut<Cart>, item: string): void {
cart.items.push(item);
}
export function restock(cart: Mut<Cart>, items: readonly string[]): number {
for (const item of items) addItem(cart, item);
return cart.items.length;
}✅ Accepted: the call lends the cart, and says so
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Cart = { readonly id: string; items: string[] };
function addItem(cart: Mut<Cart>, item: string): void {
cart.items.push(item);
}
export function restock(cart: Mut<Cart>, items: readonly string[]): number {
for (const item of items) addItem(mut(cart), item);
return cart.items.length;
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Cart = { readonly id: string; items: string[] };
function addItem(cart: Mut<Cart>, item: string): void {
cart.items.push(item);
}
export function restock(cart: Mut<Cart>, items: readonly string[]): number {
for (const item of items) addItem(cart, item);
return cart.items.length;
}mut(value) returns value itself, and the safe-ts transform erases the call (mut(cart) becomes cart), so it costs nothing at run time; code that runs without the transform works too. With the prelude, mut needs no import (a Vue template still imports it, since templates don't see the prelude's names).
What it reports
An argument at a parameter declared Mut<...> that isn't written mut(...), where the checker knows what the call runs: a function or arrow of this file or another, a nested function, a method of a class (new Ledger().record(cart), this.repo.save(cart), a field declared with the class), a member of a module-level object, and a parameter or local whose declared type is a function type (visit: (account: Mut<Account>) => void). Also a JSX prop of a component whose props type declares it Mut<...> (<Editor draft={draft} />), and a Vue template's binding to a child component's prop declared Mut<...> in its defineProps<...>() (<Field :value="draft" />).
A value put into a place declared Mut<...> without a call is reported too, where it is written: a variable's initializer or a later assignment to it, a parameter's default, a class field's initializer or a write to this.field, a return in a function declared to return Mut<...> (also Promise<Mut<...>> of an async one and a generator's result), and each element of an array literal passed where readonly Mut<T>[] is asked.
The value doesn't need to be a Mut already to be reported: a Mut parameter passed on (addItem(cart, item) with cart: Mut<Cart>) compiles, since a Mut<Cart> is a Mut<Cart>, and is still reported, so that every call that lends shows it. undefined passed to an optional Mut parameter (Option<Mut<T>>) isn't, nor a choice between a loan and undefined (ready ? mut(cart) : undefined).
Quick fix
Lend it: mut(cart) wraps the argument, and imports mut where the file has neither an import of it nor the prelude (an import type { Mut } becomes import { type Mut, mut }). safe-ts migrate mut <dirs> --write applies it to every call of a program in one pass: the upgrade to the nominal Mut<T> (Migrating with codemods).
A fresh literal ({ ... }, [], new Map()) is lent with the place's type written, which TypeScript would otherwise infer from the literal alone ([] is a never[]): the fix is Lend it: mut<Cost>({ ... }). mut takes that one type argument for a fresh value, since nothing else holds it (no-unchecked-cast reports mut<T>(value) of anything else). The type comes from the parameter's declaration, and is written only where this file can name it; [] or {} whose type isn't in scope gets no fix:
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Purse = { coins: number };
function collect(out: Mut<number[]>, count: number): number[] {
for (let i = 0; i < count; i++) out.push(i);
return out;
}
export function firstTen(): number[] {
return collect(mut<number[]>([]), 10);
}
export function emptyPurse(): Mut<Purse> {
return mut<Purse>({ coins: 0 });
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Purse = { coins: number };
function collect(out: Mut<number[]>, count: number): number[] {
for (let i = 0; i < count; i++) out.push(i);
return out;
}
export function firstTen(): number[] {
return collect([], 10);
}
export function emptyPurse(): Mut<Purse> {
return { coins: 0 };
}What the rule can't see
- A call the checker can't follow: a method on a value of unknown type, a function handed in without a declared type, a package's function. TypeScript still refuses the call.
safe-ts migrate mutlists the calls it couldn't follow that are named like a function takingMut. - A
Mutplace the checker can't see the type of: a field declaredMut<T>filled in an object literal (return { parent }where the declared result hasparent: Mut<...>), a local whose type is inferred (let target = tree, thentarget = next), elements built by a call (mutEach(items), oritems.map((item) => mut(item))). TypeScript refuses those withoutmut(...). A list whose declared type has plain elements (units: Unit[],new Map<K, V>()) passed there is reported, with Lend each element: mutEach(units) as its fix. - A callback in a
Mutslot: a function takingMut<T>passed where the slot's type doesn't sayMut(accounts.forEach(drain)) is TypeScript's to refuse, and it does.
Related: no-param-mutation, where a loan of a borrowed parameter is reported; aliased-mut; unneeded-mut (idiomatic), for mut(...) where the parameter isn't Mut; guide: State without globals.