Interface बनाम Type Alias
In this page:
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
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
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
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
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
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);
- एक
typealias को उसी नाम से दो बार redeclare करना, जो एक error है, जबकि duplicate interfaces merge होते हैं। interface Status = "a" | "b"जैसा एक union लिखने की कोशिश करना, जो interfaces express नहीं कर सकते।- एक type extend करने के लिए
&उपयोग करना और conflicting property types से surprised होना जोneverproduce करते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: