Function Overloading की जानकारी
In this page:
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
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
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
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
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
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));
- सिर्फ overload signatures लिखना और उनके नीचे single implementation भूल जाना।
- Implementation signature को बहुत narrow बनाना, तो यह सभी overloads से match नहीं करता।
- यह उम्मीद करना कि 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 करने देते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: