← Back to TypeScript Course | Chapter 23: Performance and Best Practices | Lesson 5 of 7

Interface बनाम Type Alias - कब उपयोग करें

Interfaces और type aliases दोनों object shapes describe कर सकते हैं, लेकिन उनकी अलग strengths हैं। Interfaces extendable object contracts के लिए अक्सर convenient होते हैं, जबकि type aliases unions, tuples, और type composition के लिए खासकर उपयोगी होते हैं।
Syntax
typescript
interface InterfaceName {
    property: type;
}

type AliasName = TypeA | TypeB;   // unions and intersections need type

Object Contracts के लिए Interfaces

Interfaces एक object की shape या एक class के public API describe करने के लिए natural choice हैं, क्योंकि वे declaration merging support करते हैं और एक contract की तरह clearly पढ़ते हैं जिसे दूसरा code implement करता है।

उदाहरण: Interfaces for Object Contracts

typescript
interface User {
  name: string;
  age: number;
}
const u: User = { name: "Ravi", age: 25 };
console.log(u);

Unions के लिए Type Aliases

Type aliases एक बार आपको एक union या intersection नाम देना हो तो बेहतर fit हैं, क्योंकि interfaces सिर्फ एक single object shape describe कर सकते हैं और "इनमें से एक" express नहीं कर सकते।

उदाहरण: Type Aliases for Unions

typescript
type Status = "pending" | "done" | "failed";
const s: Status = "pending";
console.log(s);

Types Extend और Compose करना

Interfaces और type aliases दोनों दूसरी shapes extend करना support करते हैं, लेकिन interfaces extends उपयोग करते हैं और एक ही नाम की कई declarations merge कर सकते हैं, जबकि type aliases इसके बजाय & intersection operator से shapes combine करते हैं।

उदाहरण: Extending and Composing Types

typescript
interface Base { id: number; }
interface User extends Base { name: string; }
type Tagged = Base & { tag: string };
const u: User = { id: 1, name: "Ravi" };
console.log(u);

Tuples और Function Types

Type aliases एक tuple की fixed-length, fixed-position shape या एक function की call signature सीधे नाम दे सकते हैं, जो इनमें से किसी को एक interface में wrap करने से ज़्यादा naturally पढ़ता है।

उदाहरण: Tuples and Function Types

typescript
type Point = [number, number];
type Callback = (value: number) => void;
const p: Point = [1, 2];
const log: Callback = (v) => console.log(v);
log(p[0]);

Practical Selection Rules

एक rule of thumb के रूप में: एक interface की ओर पहुंचें जब एक object या class shape model कर रहे हों जो बाद में extend हो सकती है, और unions, tuples, primitives, या किसी ऐसी चीज़ के लिए type alias की ओर पहुंचें जो plain object contract नहीं है।

उदाहरण: Practical Selection Rules

typescript
interface User { name: string; } // object contract -> interface
type Id = string | number;        // union -> type alias
const u: User = { name: "Ravi" };
const id: Id = 42;
console.log(u, id);
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. एक interface से union लिखने की कोशिश करना, जैसे interface Status = "a" | "b", जो एक syntax error है, क्योंकि unions को type चाहिए।
  2. एक ही नाम से दो type aliases declare करना और उम्मीद करना कि वे interfaces की तरह merge हो जाएंगे।
  3. एक project के across similar objects के लिए बिना consistent rule के interface और type mix करना।
🔒

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.