CodePark/ questions / Classes & enums
START PRACTICINGPRACTICE
Frontend question · TypeScript › Classes & enums

Which line produces a compile error?

class Account {
  private balance = 100;
  #pin = 1234;
}
const acc = new Account();
acc.balance;       // Line 6
acc["balance"];    // Line 7
Only Line 6

Only Line 6 errors, because balance is private.

"Both lines" is wrong. TypeScript allows bracket notation on purpose, as an escape hatch for
private members, so Line 7 compiles. This shows private is only a compile-time check. The #pin field is different: the runtime enforces it, and code outside the class cannot reach it at all.