Question presented to candidate:
"What access modifiers does TypeScript give you for class members, what does each one allow, and does private actually keep data private?"
What a strong answer should cover:
public(the default),protected(the class and its subclasses) andprivate(the declaring class only), plusreadonly(assign only in the declaration or constructor)- Parameter properties:
constructor(private readonly id: string)declares and assigns a field in one step - These modifiers are compile-time only: they are erased, so
privatefields show up inJSON.stringify,Object.keysand bracket access, and code that ignores tsc can still change them - JavaScript
#privatefields (supported since TypeScript 3.8) are enforced by the engine at run time;#x in objis a brand check (TypeScript 4.5) - Details interviewers probe:
protectedaccess only through an instance of the accessing class (TS2446), private constructors for singletons and factories, and that private members make classes compare nominally
Code / implementation expected: Optional β a small class hierarchy showing what each modifier allows and what still happens at run time.