TypeScript Advanced Interfaces
Deep dive into interfaces: extension, merging, optional and readonly fields, and real API modeling.
Interfaces describe the shape of objects and help teams agree on data contracts. ## Extending interfaces ```ts interface Person { name: string; } interface Employee extends Person { id: number; role: string; } const emp: Employee = { name: "Tom", id: 1, role: "Dev" }; ``` ## Optional and readonly ```ts interface Config { readonly host: string; port?: number; } ``` ## Interface merging Same name interfaces merge automatically. ```ts interface User { name: string; } interface User { age: number; } const u: User = { name: "Sarah", age: 25 }; ``` ## Why useful? - API contracts - shared models - plugin systems ## Graph ```mermaid flowchart TD A[Base Interface] --> B[Extended Interface] B --> C[Concrete Object] ``` ## Remember - Interfaces define contracts - They can extend and merge - Great for object shapes