← Back to TypeScript Course | Chapter 3: Variables and Functions | Lesson 10 of 10

Function Overloading की जानकारी

Function overloading TypeScript को एक ही function को call करने के कई valid तरीके describe करने देता है। हर overload एक अलग call signature provide करता है जबकि एक single implementation असली logic handle करता है।
Syntax
typescript
function name(param: type1): returnType1;
function name(param: type2): returnType2;
function name(param: any): any {
  // implementation
}

Basic Function Overloads

Function overloading कई overload signatures लिखने से शुरू होता है, हर एक parameter types के एक valid combination को describe करते हुए, सीधे एक single shared implementation signature के ऊपर।

Callers सिर्फ overload signatures देखते और चुनते हैं, implementation का अपना signature कभी नहीं।

उदाहरण: Basic Function Overloads

typescript
function format(value: string): string;
function format(value: number): string;
function format(value: string | number): string {
  return `Value: ${value}`;
}
console.log(format("hi"), format(5));

Overloading with Different Parameters

Overload signatures उन functions को describe कर सकते हैं जो बिल्कुल अलग संख्या या combinations के parameters accept करते हैं, जैसे एक overload एक single string लेता है और दूसरा एक string plus एक number।

TypeScript function को असल में कैसे call किया गया उसके आधार पर matching overload चुनता है।

उदाहरण: Overloading with Different Parameters

typescript
function combine(a: string): string;
function combine(a: string, b: number): string;
function combine(a: string, b?: number): string {
  return b === undefined ? a : `${a}-${b}`;
}
console.log(combine("id"), combine("id", 42));

Overload Implementation

हर overload के नीचे की implementation signature को अपने types में हर overload के promise को satisfy करने के लिए इतना broad होना चाहिए, भले ही callers उस signature को कभी सीधे call नहीं करते।

यही है जो एक single function body को हर declared overload case को सही से handle करने देता है।

उदाहरण: Overload Implementation

typescript
function toArray(value: string): string[];
function toArray(value: number): number[];
function toArray(value: string | number): (string | number)[] {
  return [value];
}
console.log(toArray("x"), toArray(1));

Overloads and Return Types

अलग overloads किस input pattern match हुआ इसके आधार पर अलग-अलग return types specify करने के लिए free हैं, जो compiler को callers को एक overly broad union type के बजाय एक precisely typed result देने देता है।

यह overloading के सबसे बड़े practical benefits में से एक है।

उदाहरण: Overloads and Return Types

typescript
function parseValue(value: string): number;
function parseValue(value: number): string;
function parseValue(value: string | number): number | string {
  return typeof value === "string" ? Number(value) : String(value);
}
console.log(parseValue("42"), parseValue(42));

When to Use Function Overloading

Function overloading का उपयोग करें जब एक single function naturally कई distinct, well-defined input patterns support करे और आप चाहें कि TypeScript callers को precise, pattern-specific type checking दे।

Simpler cases के लिए, union types या optional parameters उपयोग करने वाला एक single signature आमतौर पर maintain करना आसान होता है।

उदाहरण: When to Use Function Overloading

typescript
function makeDate(timestamp: number): Date;
function makeDate(year: number, month: number, day: number): Date;
function makeDate(a: number, b?: number, c?: number): Date {
  return b === undefined ? new Date(a) : new Date(a, b, c);
}
console.log(makeDate(2024, 0, 1));
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. सिर्फ overload signatures लिखना और उनके नीचे single implementation भूल जाना।
  2. Implementation signature को बहुत narrow बनाना, तो यह सभी overloads से match नहीं करता।
  3. यह उम्मीद करना कि implementation signature callable है, जब callers को सिर्फ overload signatures visible होते हैं।
चैप्टर सारांश
  • let और const को type annotations के साथ उपयोग करें, और जब types obvious हों तो type inference को अपना काम करने दें।
  • Functions अपने parameters और return values टाइप कर सकते हैं, और optional, default, और rest parameters support करते हैं।
  • Arrow functions और function overloading आपको precise types के साथ flexible function signatures describe करने देते हैं।

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.