Skip to content

Public Mutable Field (ARCH3186)

Key typescript.quality.public-mutable-field · Severity warning · Category quality

Publicly exposed mutable fields break the class’s encapsulation and let outside code assign invalid state. They should be guarded with accessors (getter/setter) or readonly.

class Account {
public balance: number = 0;
}
const a = new Account();
a.balance = -500; // unguarded access to an invalid state
class Account {
private _balance = 0;
get balance() { return this._balance; }
deposit(amount: number) {
if (amount <= 0) throw new Error("invalid");
this._balance += amount;
}
}

Make the field private and manage mutations through validating accessors or methods.