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

Interface बनाम Type Alias

Interfaces और type aliases दोनों object shapes describe कर सकते हैं, और वे अक्सर बहुत similar दिखते हैं। हालांकि, उनकी capabilities अलग हैं और वे अलग situations में उपयोगी हैं।
Syntax
typescript
interface Name {
  property: type;
}

type Name2 = {
  property: type;
};

Basic Object Shapes

Interfaces और type aliases दोनों exactly वही object structure describe कर सकते हैं, और straightforward object shapes के लिए दोनों largely interchangeable हैं। असली differences सिर्फ ज़्यादा advanced usage में दिखते हैं।

उदाहरण: Basic Object Shapes

typescript
interface UserI { name: string; }
type UserT = { name: string };
const a: UserI = { name: "Kai" };
const b: UserT = { name: "Mira" };
console.log(a, b);

Extending and Combining

Interfaces extends keyword के through grow होते हैं दूसरे interface से inherit करने के लिए, जबकि type aliases typically & intersection operator उपयोग करके कई types को साथ combine करते हैं। दोनों approaches अलग syntax के through एक similar end result achieve करते हैं।

उदाहरण: Extending and Combining

typescript
interface Animal { name: string; }
interface Dog extends Animal { breed: string; }

type AnimalT = { name: string };
type DogT = AnimalT & { breed: string };

const dog: DogT = { name: "Fido", breed: "Poodle" };
console.log(dog);

Union Types and Type Aliases

Type aliases कई possible types के एक union को directly represent कर सकते हैं, जैसे type Status = active | inactive, जो interfaces अपने आप express नहीं कर सकते क्योंकि एक interface हमेशा एक single object shape describe करता है।

यह type aliases को multiple possible forms वाली values के लिए natural choice बनाता है।

उदाहरण: Union Types and Type Aliases

typescript
type Status = 'active' | 'inactive';
let status: Status = 'active';
console.log(status);

Declaration Merging

एक ही scope में बिल्कुल एक जैसे नाम के साथ declared कई interfaces automatically एक combined interface में merge हो जाते हैं, जो एक feature है जिसे declaration merging कहते हैं।

Type aliases के पास इसका कोई equivalent नहीं है; एक type alias को उसी नाम के साथ redeclare करना simply एक compile error है।

उदाहरण: Declaration Merging

typescript
interface Box {
  width: number;
}
interface Box {
  height: number;
}
const box: Box = { width: 10, height: 20 }; // merged automatically
console.log(box);

Choosing Between Interface and Type

Interfaces reusable, extensible object contracts describe करने के लिए ज़्यादा natural लगते हैं, खासकर consumers द्वारा extend किए जाने वाले public APIs के लिए।

Type aliases unions, intersections, और दूसरी type compositions के लिए खासकर उपयोगी हैं जो interfaces directly express नहीं कर सकते।

उदाहरण: Choosing Between Interface and Type

typescript
interface ApiUser { id: number; } // extensible public contract
type Result = { ok: true } | { ok: false }; // union, needs type alias
console.log({ id: 1 } as ApiUser, { ok: true } as Result);
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. एक type alias को उसी नाम से दो बार redeclare करना, जो एक error है, जबकि duplicate interfaces merge होते हैं।
  2. interface Status = "a" | "b" जैसा एक union लिखने की कोशिश करना, जो interfaces express नहीं कर सकते।
  3. एक type extend करने के लिए & उपयोग करना और conflicting property types से surprised होना जो never produce करते हैं।
🔒

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.