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.
Incorrect
Section titled “Incorrect”class Account { public balance: number = 0;}
const a = new Account();a.balance = -500; // unguarded access to an invalid stateCorrect
Section titled “Correct”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.