🧠 Understanding Classes vs Interfaces in Angular (TypeScript)

PriyaPriya
4 min read

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

FeatureInterfaceClass
PurposeDefine shape/type onlyCreate structure + behavior
JavaScript Output❌ Compile-time onlyβœ… Exists at runtime
Instantiation❌ Not instantiableβœ… Can use new
Methods❌ Only declarationsβœ… Full implementations
Use CasesDTOs, type safetyBusiness logic, utility models
Inheritanceextends interfacesextends 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

FeatureInterface βœ…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

4
Subscribe to my newsletter

Read articles from Priya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Priya
Priya