π§ Understanding Classes vs Interfaces in Angular (TypeScript)

If youβre working with Angular β a powerful framework built with TypeScript β understanding the difference between interfaces and classes is essential.
Knowing when to use each helps you write cleaner, more maintainable code β whether you're defining data models, working with services, or building components.
Letβs explore both with practical examples and real Angular use cases.
π What Is an Interface in TypeScript?
An interface is a contract that defines the shape of an object. It exists only at compile time and doesnβt produce any JavaScript in your final bundle.
π§ Analogy:
An interface is like a blueprint β it describes what an object should look like but doesnβt actually construct it.
β Example: Interface
export interface Expense {
id: number;
category: string;
amount: number;
date: Date;
description: string;
notes?: string; // Optional
project?: string; // Optional
}
This ensures that any object labeled as Expense includes these properties.
π§± What Is a Class in TypeScript?
A class includes both the structure and the implementation. Unlike interfaces, classes are compiled into JavaScript and exist at runtime.
ποΈ Analogy:
A class is like the actual building created from the blueprint.
β Example: Class
export class Expense {
constructor(
public id: number,
public category: string,
public amount: number,
public date: Date,
public description: string,
public notes?: string,
public project?: string
) {}
getSummary(): string {
return ${this.category}: $${this.amount};
}
}
π οΈ Usage:
const myExpense = new Expense(1, 'Travel', 100, new Date(), 'Flight ticket');
console.log(myExpense.getSummary()); // "Travel: $100"
βοΈ Interface vs Class: Key Differences
Feature | Interface | Class |
Purpose | Define shape/type only | Create structure + behavior |
JavaScript Output | β Compile-time only | β Exists at runtime |
Instantiation | β Not instantiable | β Can use new |
Methods | β Only declarations | β Full implementations |
Use Cases | DTOs, type safety | Business logic, utility models |
Inheritance | extends interfaces | extends classes, implements interfaces |
π§ When Should You Use Each?
β Use Interface when:
You only need to describe data shape.
You want zero runtime overhead.
Typing API responses or data contracts (DTOs).
β Use Class when:
You need logic, methods, or computed properties.
You plan to instantiate objects.
You want default values or utility functions.
π Decision Flowchart
π
E.g., Interface β Simple Shape | Class β Need Logic & Instantiation
π Classes Implementing Interfaces
Use interfaces for type safety and classes for behavior:
export interface ExpenseShape {
id: number;
category: string;
amount: number;
date: Date;
}
export class Expense implements ExpenseShape {
constructor(
public id: number,
public category: string,
public amount: number,
public date: Date
) {}
getSummary(): string {
return ${this.category}: $${this.amount};
}
}
π§© Real Angular Use Cases
π‘ Using Interface in a Service
@Injectable({ providedIn: 'root' })
export class ExpenseService {
constructor(private http: HttpClient) {}
getExpenses(): Observable<Expense[]> {
return this.http.get<Expense[]>('/api/expenses');
}
}
π‘ Using Class with Behavior
export class Expense {
constructor(
public id: number,
public category: string,
public amount: number,
public date: Date,
public description: string,
public notes?: string
) {}
getSummary(): string {
return ${this.category}: $${this.amount};
}
}
π¨βπ©βπ§ Inheritance with Interfaces & Classes
β Interface Inheritance
interface Timestamped {
createdAt: Date;
}
interface Expense extends Timestamped {
id: number;
amount: number;
}
β Class Inheritance
class BaseExpense {
constructor(public id: number) {}
getId() {
return this.id;
}
}
class DetailedExpense extends BaseExpense {
constructor(id: number, public amount: number) {
super(id);
}
}
π Summary: Interface vs Class
Feature | Interface β | Class β |
Type-only contract | β | β |
Compiles to JavaScript | β | β |
Constructors & Methods | β | β |
Supports Inheritance | β | β |
Used at Runtime | β | β |
Ideal for DTOs/API Typing | β | β |
Useful for Logic/Behavior | β | β |
π― Final Tip
β Use interfaces when you need lightweight, type-safe structures.
β Use classes when you want to encapsulate logic, instantiate objects, or add behavior inside Angular components or services.
π Was This Helpful?
If this helped clarify the difference between classes and interfaces in Angular, consider sharing it or dropping a comment with your thoughts!
Letβs make TypeScript more approachable β one concept at a time π‘
π Further Reading
Subscribe to my newsletter
Read articles from Priya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
