Skip to content
BackendFrontend

3. TypeScript Interfaces vs Type Alias in Hindi – Advanced Examples के साथ 2026

April 7, 2026 6 min read

नमस्ते दोस्तों! 🙏

स्वागत है The Easy Master पर!

आज हम बात करेंगे TypeScript के दो सबसे important concepts की – Interfaces और Type Aliases।

बहुत सारे students मुझे message करते हैं: “भैया, TypeScript में Interface और Type Alias में क्या फर्क है? दोनों एक जैसे लगते हैं। कौनसा use करना सही है?”

मैं समझता हूँ यह confusion। जब मैंने खुद TypeScript सीखी, तो ये दोनों चीज़ें मुझे भी एक जैसी लगती थीं। लेकिन जब real projects में काम किया, तो फर्क पता चला।

इस article में TypeScript interfaces vs type alias in Hindi को advanced examples के साथ cover करूंगा। तुम्हें पता चलेगा कि कौन सी situation में क्या use करना है, और क्यों।

तो चलिए शुरू करते हैं बिना time waste किए!

1. Interface क्या होता है?

Interface TypeScript का एक ऐसा तरीका है जिससे हम किसी object की shape define कर सकते हैं। मतलब ये बताना कि object में कौन से properties होंगे और उनके data types क्या होंगे।

Simple example देखो:

Code
interface Student {
  name: string;
  age: number;
  course: string;
}

const raj: Student = {
  name: "Raj Kumar",
  age: 21,
  course: "B.Tech CSE"
};

यहाँ Student interface ने clear कर दिया कि student object में name (string), age (number), और course (string) होना चाहिए। अगर कोई property missing होगी या type गलत होगी, TypeScript error दे देगा।

Interface का सबसे बड़ा feature है extend करना – एक interface दूसरे interface से properties inherit कर सकता है।

और declaration merging – same naam का interface दोबारा लिख सकते हो, दोनों merge हो जाएंगे।

याद रखो: Interface sirf objects की shape define करने के लिए बना है। Functions, arrays, tuples, union types के लिए ये उतना flexible नहीं है।


2. Type Alias क्या होता है?

Type Alias एक nickname है किसी भी type का। तुम type keyword का use करके किसी भी complex type को एक naam दे सकते हो।

ये object shape के अलावा union types, tuple types, function types, primitive types – सब कुछ define कर सकता है।

Example:

Code
// Primitive type alias
type UserID = string | number;

// Object type alias
type Employee = {
  id: number;
  name: string;
  department: string;
};

// Union type alias
type Status = "active" | "inactive" | "pending";

// Function type alias
type GreetFunction = (name: string) => string;

Type alias में extend का feature नहीं है, लेकिन intersection (&) का use करके multiple types combine कर सकते हैं।

याद रखो: Type alias किसी भी चीज़ का alias bana सकता है – primitive से लेकर complex mapped types तक। लेकिन ये declaration merging support नहीं करता।


3. TypeScript Interfaces vs Type Alias – Direct Comparison

TypeScript interfaces vs type alias in Hindi

ये table देख लो – TypeScript interfaces vs type alias in Hindi में clear हो जाएगा:

FeatureInterfaceType Alias
Object shapes define करना✅ Best✅ Possible
Primitive types (string, number)❌ Nahi✅ Yes
Union types (string | number)❌ Nahi✅ Yes
Tuple types❌ Nahi✅ Yes
Extend करना✅ extends keyword✅ Intersection (&)
Declaration merging✅ Yes (same naam dobara likh sakte ho)❌ No
Mapped types❌ Limited✅ Full support
Conditional types❌ Nahi✅ Yes
Recursive types❌ Difficult✅ Easy
Performance (error messages)✅ Cleaner🟡 Thoda verbose
Recommended for libraries/APIs✅ Yes🟡 Depends

मेरा personal experience: जब मैंने एक large project में काम किया, तो मैंने शुरू में सिर्फ type aliases use किए। बाद में जब मुझे declaration merging की ज़रूरत पड़ी, तो मुझे सब कुछ rewrite करना पड़ा। तो सीखा – interfaces का use objects के लिए करो, types का use बाकी सबके लिए।


4. Advanced Examples

अब हम advanced examples देखेंगे जहाँ TypeScript interfaces vs type alias in Hindi का real फर्क समझ आएगा।

Declaration Merging

ये feature सिर्फ interface में है। Type alias में नहीं।

Code
// पहली बार interface define किया
interface User {
name: string;
}

// दूसरी बार same naam से interface define किया
interface User {
age: number;
}

// अब User के पास दोनों properties होंगी
const user: User = {
name: "Amit",
age: 22
}; // कोई error नहीं

Type alias में ये possible नहीं:

Code
type User = { name: string; };
type User = { age: number; }; // ❌ Error: Duplicate identifier 'User'

Use case: जब तुम third-party libraries के types extend कर रहे हो या multiple files में types add कर रहे हो।

Extends vs Intersection

Interface extends use करता है:

Code
interface Animal {
  name: string;
}

interface Dog extends Animal {
  breed: string;
}

const myDog: Dog = { name: "Tommy", breed: "Labrador" };

Type Alias & (intersection) use करता है:

Code
type Animal = { name: string; };
type Dog = Animal & { breed: string; };
const myDog: Dog = { name: "Tommy", breed: "Labrador" };

फर्क कब आता है? जब conflicting properties हों:

Code
interface A { value: string; }
interface B { value: number; }
interface C extends A, B {} // ❌ Error: value type mismatch

type D = A & B; // ✅ No error, value becomes `never`

Mapped Types with Type Alias

Type alias में mapped types का full support है। Interface में limited है।

Code
// सभी properties को optional बनाना
type PartialUser = {
  [K in keyof User]?: User[K];
};

// सभी properties को readonly बनाना
type ReadonlyUser = {
  readonly [K in keyof User]: User[K];
};

// Real example
type Todo = {
  title: string;
  completed: boolean;
};

type PartialTodo = Partial<Todo>; // Built-in mapped type

Interface के साथ ये directly possible नहीं है।

Conditional Types

ये सिर्फ type alias में possible है।

Code
type IsString<T> = T extends string ? "Yes" : "No";

type A = IsString<string>; // "Yes"
type B = IsString<number>; // "No"

// Advanced example – function return type based on input
type ReturnTypeBasedOnInput<T> = T extends string 
  ? string 
  : T extends number 
    ? number 
    : never;

function process<T>(input: T): ReturnTypeBasedOnInput<T> {
  // implementation
  return input as any;
}

Recursive Types

Type alias recursive structures आसानी से बना सकता है। Interface में मुश्किल है।

Code
// JSON value का recursive type
type JSONValue = 
  | string
  | number
  | boolean
  | null
  | JSONValue[]
  | { [key: string]: JSONValue };

// Recursive tree structure
type TreeNode<T> = {
  value: T;
  children?: TreeNode<T>[];
};

const tree: TreeNode<number> = {
  value: 1,
  children: [
    { value: 2 },
    { value: 3, children: [{ value: 4 }] }
  ]
};

5. Practical Cheat Sheet – कब क्या use करें?

TypeScript interfaces vs type alias in Hindi

ये quick guide देख लो – कभी confusion नहीं होगी:

अगर तुम्हें चाहिए…तो use करो…
Object / class की shapeInterface (recommended)
Union type (string | number)Type Alias
Tuple type ([string, number])Type Alias
Declaration mergingInterface (only option)
Mapped typesType Alias
Conditional typesType Alias
Recursive type (like JSON)Type Alias
Function typeType Alias (cleaner)
Primitive type ka aliasType Alias
Library/API type exportInterface (better error messages)

मेरा rule of thumb:

  • By default Interface use करो – क्योंकि ये extensible है और better error messages देता है।
  • Type Alias use करो जब: union, tuple, mapped, conditional, recursive types चाहिए।

6. Common Mistakes और उनके Solutions

Mistake 1: Interface में union type डालने की कोशिश करना

Code
// ❌ गलत
interface Status = "active" | "inactive"; // Syntax error

// ✅ सही – type alias use करो
type Status = "active" | "inactive";

Mistake 2: Declaration merging को type alias के साथ use करना

Code
// ❌ गलत
type Config = { url: string; };
type Config = { timeout: number; }; // Error

// ✅ सही – interface use करो
interface Config { url: string; }
interface Config { timeout: number; } // Merges

Mistake 3: Recursive interface बनाने की कोशिश

Code
// ❌ ये काम नहीं करेगा ठीक से
interface Recursive {
  value: number;
  next: Recursive | null; // Possible but tricky
}

// ✅ सही – type alias better है
type Recursive = {
  value: number;
  next?: Recursive;
};

Mistake 4: सब कुछ type alias में लपेटना

Code
// ❌ Overkill
type Name = string;
type Age = number;
type User = { name: Name; age: Age; };

// ✅ Simple and clean
interface User {
  name: string;
  age: number;
}

7. Frequently Asked Questions

Q1: क्या मैं interface में union type use कर सकता हूँ property के रूप में?
हाँ, property के रूप में use कर सकते हो: interface User { status: "active" | "inactive"; } लेकिन पूरा interface union type नहीं बना सकते।

Q2: क्या type alias को extend किया जा सकता है?
Type alias में extends keyword नहीं है, लेकिन intersection (&) से same effect मिलता है।

Q3: Performance में क्या फर्क है?
Interfaces थोड़े faster हैं और error messages ज्यादा clean आती हैं। Type alias में complex types पर error messages थोड़ी लंबी हो सकती हैं।

Q4: क्या मैं class में interface और type alias दोनों implement कर सकता हूँ?
हाँ, class implements interface और type alias दोनों को कर सकती है – बशर्ते वो object shape define करते हों।

Q5: Third-party library में कौनसा use होता है?
ज्यादातर popular libraries (React, Node types) interfaces use करती हैं क्योंकि declaration merging से user types extend कर सकते हैं।

Q6: क्या मैं type alias में function overload कर सकता हूँ?
Function overload सिर्फ interface और function signatures में possible है। Type alias में multiple call signatures लिख सकते हो लेकिन थोड़ा complex है।

Q7: क्या TypeScript 5.x में कोई नया change आया है?
TypeScript 5.x में दोनों में कोई बड़ा breaking change नहीं आया है। Performance improvements आए हैं लेकिन use-case same है।

Q8: Mapped types कब use करने चाहिए?
जब आपको किसी existing type के सभी properties को modify करना हो – जैसे सबको optional या readonly बनाना। तब type alias with mapped types best है।

Q9: क्या मैं दोनों को एक साथ use कर सकता हूँ?
बिल्कुल। एक interface में type alias को property के तौर पर use कर सकते हो, और type alias में interface को भी।

Q10: JSON data के लिए क्या use करूँ?
JSON dynamic और recursive होता है – तो type alias better choice है।


8. Conclusion और Call to Action

तो दोस्तों, आज हमने TypeScript interfaces vs type alias in Hindi में बहुत गहराई से समझा।

Quick summary:

  • Interface = objects के लिए best, extensible, declaration merging, clean error messages
  • Type Alias = union, tuple, mapped, conditional, recursive types के लिए best

Golden rule:

By default Interface use करो। जब union/mapped/conditional चाहिए, तो Type Alias use करो।

मैंने खुद इस rule को follow करके कई large TypeScript projects लिखे हैं और बहुत headaches बची हैं।

अब तुम्हारी बारी है।


Resources

Additional Resources

TheEasyMaster

Author at The Easy Master.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *