Careeroza
Extending Interfaces
extends
An interface can extends one or more other interfaces and add its own properties.
Example
interface Entity {
id: number;
createdAt: string;
}
interface Person {
name: string;
email: string;
}
interface Customer extends Entity, Person {
loyaltyPoints: number;
}
const c: Customer = {
id: 1, createdAt: "2026-01-15T10:00:00Z",
name: "Vikram", email: "[email protected]",
loyaltyPoints: 320,
};
function welcome(p: Person): string {
return "Welcome back, " + p.name;
}
console.log(welcome(c)); // a Customer is also a Person
console.log(c.loyaltyPoints);Output
Welcome back, Vikram 320
Structural Typing
TypeScript checks the shape, not the name. Any object that has the needed properties fits, even if it never mentions the interface. This is called structural typing ("duck typing").
Example
interface HasPrice {
price: number;
}
function withGst(item: HasPrice): number {
return Math.round(item.price * 1.18);
}
const book = { title: "Clean Code", price: 500 };
const course = { name: "TypeScript", price: 1999, hours: 20 };
console.log(withGst(book), withGst(course));Output
590 2359