TypeScript Method Overloading In Hindi

📔 : TypeScript 🔗

एक ऐसा feature जो TypeScript को और powerful बनाता है वो है Method Overloading. Method overloading से हम एक ही method के multiple versions define कर सकते हैं, जो अलग-अलग parameter types या counts को handle करते हैं।

इस topic में हम TypeScript Method Overloading को detail में समझेंगे, उसके syntax, use cases, और examples के साथ।

What is Method Overloading ?

Method overloading का मतलब है एक ही नाम के multiple methods define करना लेकिन उनके parameters अलग hone चाहिए।

इसका advantage यह होता है कि हम एक method को multiple तरीकों से use कर सकते हैं बिना new method names बनाये।

TypeScript में method overloading का concept JavaScript के upper layer पर implement होता है, क्योंकि JavaScript खुद method overloading को support नहीं करता।

TypeScript compiler हमारे overloads को check करता है और ensure करता है कि method को सही parameters के साथ call किया जा रहा है। और classes के अलावा इसे आप normally functions में भी apply कर सकते हैं।

TypeScript Function Overloading

सबसे पहले हम function overloading का एक example देख लेते हैं फिर हम class के अदंर method overloading का example देखेंगे।

function greet(name: string): void; function greet(name: string, age: number): void; function greet(name: string, age?: number): void { if (age !== undefined) { console.log(`Hello ${name}, you are ${age} years old.`); } else { console.log(`Hello ${name}`); } } greet("John"); // Output: Hello John greet("John", 25); // Output: Hello John, you are 25 years old.

यहाँ greet() function को दो तरीके से overload किया गया है। पहला overload सिर्फ एक name parameter लेता है, और दूसरा overload एक optional age parameter भी लेता है।

TypeScript Method Overloading In Classes

TypeScript में classes के अंदर भी method overloading का use किया जा सकता है। Classes में methods को overload करने का process functions के जैसे ही होता है।

मतलब कि, आप multiple method signatures define करते हैं, और फिर एक implementation provide करते हैं जो सब overloads को support करे।

TypeScript Method Overloading Example

class Person { // Overloaded signatures sayHello(name: string): void; sayHello(name: string, age: number): void; // Single implementation sayHello(name: string, age?: number): void { if (age !== undefined) { console.log(`Hello, ${name}. You are ${age} years old.`); } else { console.log(`Hello, ${name}.`); } } } const person = new Person(); person.sayHello("John"); // Output: Hello, John. person.sayHello("John", 25); // Output: Hello, John. You are 25 years old.

इस example में sayHello() method को overload किया गया है जिसमे एक overload सिर्फ name लेता है, और दूसरा name के साथ age भी लेता है।

ध्यान रहे - TypeScript में overloading का concept थोड़ा अलग है , बाकी programming languages में method overloading में हर method का अपना खुद का implementation और logic होता है। जबकि TypeScript में Overloaded signatures को define किया जाता है उनका implementation नहीं। आपको एक single implementation करना होता है जो सभी overloads को handle करेगा। जैसा अभी तक आपने examples में देखा।

Advantages of Method Overloading

  • Code Reusability : एक method के multiple versions hone से code re-usability बढ़ती है। आपको new method names बनाने कि जरूरत नहीं होती, और एक ही method के different functionalities हो सकती हैं।

  • Flexibility : Users को methods को अलग-अलग तरीके से call करने कि flexibility मिलती है , जैसे कि एक parameter के साथ या multiple parameters के साथ।

  • Type Safety : TypeScript method overloading के साथ type safety ensure करता है , Compiler check करता है कि method को सही parameters के साथ call किया जा रहा है या नहीं।

Limitations of Method Overloading In TypeScript

TypeScript method overloading काफी powerful feature है, लेकिन कुछ limitations भी हैं -

1. No Return Type Overloading

TypeScript में आप different return types के साथ methods को overload नहीं कर सकते। सब overloads का return type same होना चाहिए।

// This is NOT allowed
function getData(id: number): string;
function getData(id: number): number;
2. Actual Implementation Must Handle All Cases

Overloaded signatures के अलग-अलग implementations नहीं हो सकते। एक single implementation overloads के सब cases को handle करेगा।

Quick Recap

  • Method overloading का मतलब है एक method के multiple signatures define करना लेकिन implementation एक ही होती है।

  • Overloading से आप methods को अलग-अलग parameter types या counts के साथ call कर सकते हैं।

  • Classes और functions दोनो में method overloading का use हो सकता है।

  • Optional parameters का use भी overloading का एक तरीका है।

I Hope , आपको TypeScipt में Method overloading समझ आ गया होगा।

Happy coding 😍

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook