Extending Interfaces in TypeScript
Extending Interfaces in TypeScript
Section titled “Extending Interfaces in TypeScript”Basic Extension
Section titled “Basic Extension”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!"),};Multiple Inheritance
Section titled “Multiple Inheritance”interface Flyable { fly(): void; altitude: number;}
interface Swimmable { swim(): void; depth: number;}
interface Duck extends Flyable, Swimmable { quack(): void;}
// Conflict resolutioninterface A { shared: string; a(): void }interface B { shared: string; b(): void }interface C extends A, B { } // OK: shared is compatibleOverriding Properties
Section titled “Overriding Properties”interface Base { id: string; value: unknown;}
interface Detailed extends Base { value: { name: string; count: number }; // Narrowing description: string;}Real Project Example
Section titled “Real Project Example”// API response hierarchyinterface 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;Interview Questions
Section titled “Interview Questions”Easy: How do you extend an interface?
Medium: Can an interface extend multiple interfaces?
Hard: How are property conflicts resolved when extending multiple interfaces?