TypeScript Tutorial: Classes
TypeScript classes with access modifiers, readonly, abstract classes, and interface implementation.
Class Basics
TypeScript adds access modifiers and type annotations to JavaScript classes:
```typescript
class Person {
name: string;
private age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet(): string {
return `Hi, I'm ${this.name}`;
}
}
const p = new Person("Alice", 30);
console.log(p.greet()); // "Hi, I'm Alice"
console.log(p.name); // ✓
console.log(p.age); // ✗ Error: 'age' is private
```
---
Access Modifiers
| Modifier | Accessible from |
|---|---|
| `public` | Anywhere (default) |
| `private` | Only within the class |
| `protected` | Within the class and subclasses |
| `readonly` | Anywhere but not reassignable |
---
Constructor Shorthand
Declare and assign in one step:
```typescript
class Point {
constructor(
public readonly x: number,
public readonly y: number
) {}
distance(): number {
return Math.sqrt(this.x ** 2 + this.y ** 2);
}
}
const p = new Point(3, 4);
console.log(p.distance()); // 5
```
---
Implementing Interfaces
```typescript
interface Printable {
print(): void;
}
class Invoice implements Printable {
constructor(private amount: number) {}
print(): void {
console.log(`Invoice: $${this.amount}`);
}
}
```
---
Abstract Classes
Abstract classes define a template — they can't be instantiated directly:
```typescript
abstract class Animal {
abstract sound(): string;
describe(): string {
return `I make the sound: ${this.sound()}`;
}
}
class Dog extends Animal {
sound(): string { return "Woof"; }
}
const dog = new Dog();
console.log(dog.describe()); // "I make the sound: Woof"
```
---
What's Next?
Next: **Enums** — named constant sets for representing fixed choices.
What you'll learn in this TypeScript classes tutorial
This interactive TypeScript tutorial has 13 hands-on exercises. Estimated time: 15 minutes.
- Defining a class with typed properties — A TypeScript class declares its properties and their types at the top of the class body. TypeScript checks that the cons…
- Constructor shorthand — declare and assign in one step — Prefixing constructor parameters with `public`, `private`, or `readonly` automatically declares them as class properties…
- Access modifiers — public, private, protected — Access modifiers control who can access a class member. `public` (default) — anyone. `private` — only the class itself. …
- Getters and setters — controlled property access — Getters (`get`) and setters (`set`) let you run logic when a property is read or written. They look like properties to c…
- Static members — class-level state and methods — Static members belong to the class itself, not instances. They're used for utility methods, factories, and shared state …
- Inheritance — extending classes — TypeScript classes can extend other classes. The subclass inherits all public and protected members and can override met…
- Abstract classes — enforce subclass contracts — Abstract classes define the structure that subclasses must implement. You can't instantiate an abstract class directly —…
- Implementing interfaces — explicit contracts — A class can implement one or more interfaces. This explicitly declares that the class fulfills a contract — TypeScript t…
- Private fields (#) — truly private at runtime — TypeScript's `private` keyword is compile-time only. JavaScript's `#` fields are truly private at runtime — they cannot …
- Mixins — composing class behavior — Mixins add reusable behaviors to classes without deep inheritance hierarchies. TypeScript supports mixin patterns throug…
- Class decorators — metadata and transformation — Decorators (experimental, widely used in frameworks like Angular and NestJS) add metadata or transform a class. TypeScri…
- Type-safe event emitter class — A common pattern is building a type-safe event emitter where TypeScript knows exactly what payload each event carries. T…
- Class with generic type parameter — Classes can have generic type parameters, just like functions and interfaces. This lets you build reusable data structur…
TypeScript Classes concepts covered
- Class Basics
- Access Modifiers
- Constructor Shorthand
- Implementing Interfaces
- Abstract Classes
- What's Next?