TypeScript classes add type checking to JavaScript class syntax. They help you model objects that combine data and behavior, such as users, invoices, carts, repositories, services, UI components, and domain entities.
A TypeScript class can use constructors, public/private/protected members, readonly properties, getters, setters, static members, abstract classes, inheritance, and `implements` clauses. The goal is not to put everything into classes; the goal is to use classes when encapsulated state and behavior make the code clearer.
A class is a blueprint for objects. The constructor initializes each instance, methods define behavior, and properties store instance data. TypeScript checks that values assigned to properties match their declared types.
class Course {
private enrolled = 0;
constructor(
public readonly id: number,
public title: string,
private maxStudents: number
) {}
enroll(): void {
if (this.enrolled >= this.maxStudents) {
throw new Error("Course is full");
}
this.enrolled++;
}
get seatsLeft(): number {
return this.maxStudents - this.enrolled;
}
}
const course = new Course(1, "TypeScript Classes", 30);
course.enroll();
console.log(course.title, course.seatsLeft);
The `implements` keyword checks that a class follows an interface shape. It does not copy code from the interface; it only verifies that the required members exist with compatible types.
interface NotificationSender {
send(to: string, message: string): Promise<void>;
}
class EmailSender implements NotificationSender {
constructor(private fromAddress: string) {}
async send(to: string, message: string): Promise<void> {
console.log(`Email from ${this.fromAddress} to ${to}: ${message}`);
}
}
async function notifyUser(sender: NotificationSender) {
await sender.send("student@example.com", "Your course is ready.");
}
notifyUser(new EmailSender("noreply@tutorialslogic.com"));
An abstract class is useful when related classes share some implementation but must define certain methods themselves. It can hold common properties and methods while leaving specialized behavior to subclasses.
abstract class ReportExporter {
constructor(protected fileName: string) {}
abstract extension(): string;
abstract render(): string;
outputPath(): string {
return `${this.fileName}.${this.extension()}`;
}
}
class CsvExporter extends ReportExporter {
extension(): string {
return "csv";
}
render(): string {
return "name,total\nAsha,1200";
}
}
const exporter = new CsvExporter("orders");
console.log(exporter.outputPath());
console.log(exporter.render());
They use JavaScript class runtime behavior, but TypeScript adds compile-time type checking, access modifiers, parameter properties, and abstract class checks.
Use functions for stateless behavior and classes when you need objects with encapsulated state and related methods.
implements is a compile-time contract.
Practice, interview questions, and compiler links for TypeScript Classes.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.