- Error
- Reported by the type checker
TS2445
Property '{0}' is protected and only accessible within class '{1}' and its subclasses.
A protected member is used outside its class and its subclasses. Use a public method instead, or move the code into a subclass.
What it means
protected members can be read inside the declaring class and inside classes that extend it. Accessing one from other code, such as a function that has an instance, is reported.
Example
class Account {
protected balance = 0;
}
const account = new Account();
console.log(account.balance);
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(5,21): error TS2445: Property 'balance' is protected and only accessible within class 'Account' and its subclasses.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Add a public method or getter that exposes what callers need.
- Move the code into a subclass, where
this.balanceis allowed. - Make the member public if it's really part of the class's API.