Severity: error
Category: access-modifiers
Stage: Stage 1
Description
SJS enforces access modifiers at compile time:
privatemembers are accessible only within the class that declares them.protectedmembers are accessible within the declaring class and its subclasses.
Accessing a private member from outside the class, or a protected member from a non-subclass
context, is a type error.
Example
// ✗ error
class Account {
private balance: number = 0
protected internalId: string = "acc-1"
}
const a = new Account()
console.log(a.balance) // SJS-E014 — private
console.log(a.internalId) // SJS-E014 — protected, not in a subclassFix
Expose state through a public accessor or method:
// ✓ correct
class Account {
private balance: number = 0
protected internalId: string = "acc-1"
getBalance(): number {
return this.balance
}
}
const a = new Account()
console.log(a.getBalance()) // ✓ public methodFor protected members, access them from a subclass:
// ✓ correct — subclass access
class SavingsAccount extends Account {
describe(): string {
return `ID: ${this.internalId}` // ✓ allowed in subclass
}
}Related codes
SJS-E015— cannot narrow an access modifier on an overriding method