Q30. What is the difference between private and #private fields?
TypeScript's private modifier primarily enforces access restrictions during type checking and is erased as a TypeScript modifier. JavaScript #field private fields have runtime-enforced private access semantics. Neither should be confused with a server-side authorization boundary.
Use #private when runtime privacy is required and the target runtime supports it; use TypeScript visibility to guide API usage.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
Which syntax uses JavaScript runtime private-field semantics?