← Back to TypeScript Course | Chapter 4: Interfaces | Lesson 4 of 7

Interfaces को Extend करना

एक interface दूसरे interface को extend कर सकता है ताकि उसकी properties और methods inherit कर सके। यह common structures को reuse करना और specialized object types बनाना आसान बनाता है।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Child interface में एक base property को एक incompatible type के साथ redefine करना, जो fail होता है।
  2. यह भूल जाना कि extended interface के object में inherited properties भी include होनी चाहिए।
  3. एक interface के लिए class ... extends syntax उपयोग करना, या extends उपयोग करने के बजाय सारे base fields दोहराना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.