Day 69 TypeScript Interfaces
Interface ek contract hai — jo bhi implement kare use yeh structure follow karna hoga. Classes aur objects ke liye interfaces define karna TypeScript best practice hai.
Interface Basics
Interface define aur implement karo.
// Interface define karo
interface Printable {
print(): void;
}
interface Serializable {
serialize(): string;
deserialize(data: string): void;
}
// Class implement kare
class Document implements Printable, Serializable {
constructor(private content: string) {}
print(): void {
console.log(this.content);
}
serialize(): string {
return JSON.stringify({ content: this.content });
}
deserialize(data: string): void {
this.content = JSON.parse(data).content;
}
}
// Interface extend karo
interface Animal {
name: string;
sound(): string;
}
interface Pet extends Animal {
owner: string;
tricks: string[];
}
const dog: Pet = {
name: "Rex",
owner: "Zohaib",
tricks: ["sit", "stay", "fetch"],
sound: () => "Woof!"
};
🎯 Practice Challenge
Repository pattern banao — IRepository<T> interface with findById, findAll, create, update, delete. UserRepository implement karo.