Skip to content

Extending Interfaces in TypeScript

interface Animal {
name: string;
age: number;
}
interface Dog extends Animal {
breed: string;
bark(): void;
}
const myDog: Dog = {
name: "Rex",
age: 3,
breed: "German Shepherd",
bark: () => console.log("Woof!"),
};
interface Flyable {
fly(): void;
altitude: number;
}
interface Swimmable {
swim(): void;
depth: number;
}
interface Duck extends Flyable, Swimmable {
quack(): void;
}
// Conflict resolution
interface A { shared: string; a(): void }
interface B { shared: string; b(): void }
interface C extends A, B { } // OK: shared is compatible
interface Base {
id: string;
value: unknown;
}
interface Detailed extends Base {
value: { name: string; count: number }; // Narrowing
description: string;
}
// API response hierarchy
interface ApiResponse {
status: number;
timestamp: string;
}
interface SuccessResponse<T> extends ApiResponse {
success: true;
data: T;
}
interface ErrorResponse extends ApiResponse {
success: false;
error: { code: string; message: string };
}
type UserResponse = SuccessResponse<User> | ErrorResponse;

Easy: How do you extend an interface?

Medium: Can an interface extend multiple interfaces?

Hard: How are property conflicts resolved when extending multiple interfaces?