Classes work like JavaScript classes, with types and access control added.
Direct answer
TypeScript classes add type annotations and access modifiers to JavaScript classes. Use public, private, protected and readonly to control access, and implements to make a class follow an interface.
Example
interface Shape {
area(): number;
}
class Rectangle implements Shape {
constructor(
private readonly width: number,
private readonly height: number
) {}
area(): number {
return this.width * this.height;
}
}
const r = new Rectangle(3, 4);
console.log(r.area()); // 12
Inheritance
class Animal {
constructor(protected name: string) {}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks`);
}
}
Notes
privateis checked by TypeScript at compile time. Use#fieldfor runtime privacy.- Prefer small classes and composition over deep inheritance.
Where Teyro fits
Teyro's TypeScript for Beginners course turns this into short lessons you can play in a few minutes a day. It has 20 lessons, and you can start free.
The bottom line
Types and access modifiers make classes clearer and safer.



