Syntax
public prop: type;
private prop: type;
protected prop: type;Examples
The Three Modifiers
Controlling exactly where each property/method can be accessed from.
class BankAccount {
public accountHolder: string;
private balance: number;
protected accountNumber: string;
constructor(holder: string, initialBalance: number, accountNumber: string) {
this.accountHolder = holder;
this.balance = initialBalance;
this.accountNumber = accountNumber;
}
public getBalance(): number {
return this.balance; // accessing private balance is fine from WITHIN the class
}
}
const account = new BankAccount("Alice", 1000, "ACC-001");
console.log(account.accountHolder); // OK - public
// console.log(account.balance); // Error - privateprotected in Subclasses
protected members remain accessible to subclasses, unlike private ones.
class SavingsAccount extends BankAccount {
showAccountNumber(): string {
return this.accountNumber; // OK - protected is accessible in subclasses
// return this.balance; // Error - private is NOT accessible, even in a subclass
}
}Best practices
- Default to private for internal implementation details, exposing only what genuinely needs to be part of the class's public API
- Use protected specifically when subclasses legitimately need access to a member that outside code should not have
- Remember these modifiers are compile-time only - they are erased when compiled to JavaScript and offer no runtime protection, unlike the # private field syntax
- Use JavaScript's native #privateField syntax instead of the private keyword when you need genuine runtime privacy, not just compile-time checking
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Typed Classes
TypeScript enhances JavaScript classes with typed properties, constructor parameters, and method signatures. Class fields must either be initialized or explicitly typed, and TypeScript checks that constructor assignments match declared property types, catching mismatches before runtime.Abstract Classes
An abstract class cannot be instantiated directly - it exists only to be extended. It can define abstract methods (a signature with no implementation, which subclasses must provide) alongside regular, fully-implemented methods that subclasses inherit as-is. This is TypeScript's way of enforcing a shared contract across a family of related classes.implements Keyword
The implements keyword declares that a class must conform to a specific interface's shape - TypeScript checks that every property and method the interface requires is actually present with a compatible type. Unlike extends, implements does not provide any inherited implementation; it is purely a compile-time contract check.Decorators
Decorators are functions that can observe, modify, or replace a class, method, property, or accessor, applied with @decoratorName syntax. They became a genuinely stable, standard part of TypeScript with TC39 Stage 3 decorator support landing as of TypeScript 5.9, and are the mechanism behind frameworks like Angular and NestJS for dependency injection and metadata-driven APIs.
TypeScript enhances JavaScript classes with typed properties, constructor parameters, and method signatures. Class fields must either be initialized or explicitly typed, and TypeScript checks that constructor assignments match declared property types, catching mismatches before runtime.Abstract Classes
An abstract class cannot be instantiated directly - it exists only to be extended. It can define abstract methods (a signature with no implementation, which subclasses must provide) alongside regular, fully-implemented methods that subclasses inherit as-is. This is TypeScript's way of enforcing a shared contract across a family of related classes.implements Keyword
The implements keyword declares that a class must conform to a specific interface's shape - TypeScript checks that every property and method the interface requires is actually present with a compatible type. Unlike extends, implements does not provide any inherited implementation; it is purely a compile-time contract check.Decorators
Decorators are functions that can observe, modify, or replace a class, method, property, or accessor, applied with @decoratorName syntax. They became a genuinely stable, standard part of TypeScript with TC39 Stage 3 decorator support landing as of TypeScript 5.9, and are the mechanism behind frameworks like Angular and NestJS for dependency injection and metadata-driven APIs.