Interfaces को Extend करना
In this page:
interface Child extends Parent {
newProperty: type;
}
Basic Interface Extension
extends keyword एक interface को दूसरे पर build करने देता है, base interface की सारी properties automatically inherit करते हुए। यह कई related interfaces में shared fields को दोहराने से बचाता है और उनके बीच के relationship को code में explicit रखता है।
उदाहरण: Basic Interface Extension
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
const dog: Dog = { name: "Rex", breed: "Labrador" };
console.log(dog);
Adding More Properties
एक interface जो दूसरे को extend करता है वह जो भी inherit करता है उसके ऊपर कितनी भी नई properties जोड़ सकता है। Extended interface को satisfy करने वाले objects को inherited properties और हर नई जोड़ी गई property दोनों include करना चाहिए।
उदाहरण: Adding More Properties
interface Person {
name: string;
}
interface Employee extends Person {
employeeId: number;
department: string;
}
const emp: Employee = { name: "Lena", employeeId: 7, department: "IT" };
console.log(emp);
Extending Multiple Interfaces
एक single interface एक साथ एक से ज़्यादा दूसरे interfaces को extend कर सकता है, उनके सारे members को एक required shape में combine करते हुए। यह कई छोटे, focused interfaces को एक बड़े contract में compose करने का एक common तरीका है।
उदाहरण: Extending Multiple Interfaces
interface Named {
name: string;
}
interface Aged {
age: number;
}
interface Person extends Named, Aged {}
const person: Person = { name: "Omar", age: 40 };
console.log(person);
Extending Interfaces with Methods
Child interfaces अपने parent interfaces पर declared किसी भी method signature को inherit करते हैं और इसके ऊपर पूरी तरह नए methods भी जोड़ सकते हैं।
यह interfaces की एक hierarchy को extend होते हुए नीचे की ओर एक increasingly specific और complete contract बनाने देता है।
उदाहरण: Extending Interfaces with Methods
interface Shape {
area(): number;
}
interface ColoredShape extends Shape {
color: string;
}
const square: ColoredShape = { color: "red", area: () => 16 };
console.log(square.color, square.area());
Building Interface Hierarchies
Interfaces को कई levels में extend करके reusable hierarchies बनाई जा सकती हैं, जहां एक base interface सबसे general shape capture करता है और नीचे का हर level ज़्यादा specific requirements जोड़ता है।
यह class inheritance जैसा है पर purely type shapes के level पर।
उदाहरण: Building Interface Hierarchies
interface Base {
id: number;
}
interface WithName extends Base {
name: string;
}
interface WithEmail extends WithName {
email: string;
}
const user: WithEmail = { id: 1, name: "Priya", email: "[email protected]" };
console.log(user);
- Child interface में एक base property को एक incompatible type के साथ redefine करना, जो fail होता है।
- यह भूल जाना कि extended interface के object में inherited properties भी include होनी चाहिए।
- एक interface के लिए
class ... extendssyntax उपयोग करना, याextendsउपयोग करने के बजाय सारे base fields दोहराना।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: