← Back to TypeScript Course | Chapter 7: Generics | Lesson 1 of 8

Generics का परिचय

Generics आपको reusable TypeScript code लिखने देते हैं जो type safety रखते हुए अलग-अलग types के साथ काम करता है। एक fixed type चुनने के बजाय, आप एक type parameter define करते हैं जो code उपयोग होने पर supply किया जा सकता है।
Syntax
typescript
function name<T>(param: T): T {
  return param;
}

Generics क्यों उपयोगी हैं

Generics एक function या class को बिना actually pass की गई specific type के बारे में information खोए कई अलग-अलग types के साथ काम करने देते हैं, any उपयोग करने के विपरीत, जो उस type information को पूरी तरह फेंक देता है।

उदाहरण: Why Generics Are Useful

typescript
function identity<T>(value: T): T {
  return value; // keeps the exact input type, unlike "any"
}
console.log(identity<string>("hello"), identity<number>(42));

Type Parameters

T जैसा एक type parameter एक concrete type के लिए एक placeholder है जो generic code actually उपयोग होने पर supply किया जाता है, उसी तरह जैसे एक function parameter call time पर supply किए गए एक concrete value के लिए एक placeholder है।

उदाहरण: Type Parameters

typescript
function wrapInArray<T>(value: T): T[] {
  return [value];
}
console.log(wrapInArray(5), wrapInArray("hi"));

Type Inference

TypeScript अक्सर supplied argument से एक generic type अपने आप infer करता है, इसलिए explicit type arguments सिर्फ तब ज़रूरी होते हैं जब compiler genuinely intended type अपने आप figure out न कर सके।

उदाहरण: Type Inference

typescript
function identity<T>(value: T): T {
  return value;
}
console.log(identity(42)); // T inferred as number, no explicit argument needed

Generic Arrays

Generics अक्सर arrays के साथ combine किए जाते हैं element type describe करने के लिए जबकि function को numbers, strings, या किसी भी दूसरी type के arrays में reusable रखते हुए, यह सब उसी generic implementation के through।

उदाहरण: Generic Arrays

typescript
function firstItem<T>(items: T[]): T {
  return items[0];
}
console.log(firstItem([1, 2, 3]), firstItem(["a", "b"]));

Generic Type Aliases

Type aliases के भी type parameters हो सकते हैं, एक object shape को अलग-अलग data types plug किए जाने के साथ reusable बनाते हुए — उदाहरण के लिए, एक generic ApiResponse<T> shape जो काम करती है चाहे T एक User हो, एक Product हो, या कुछ भी हो।

उदाहरण: Generic Type Aliases

typescript
type ApiResponse<T> = {
  data: T;
  status: number;
};
const response: ApiResponse<string> = { data: "OK", status: 200 };
console.log(response);
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. generic के बजाय any उपयोग करना, जैसे function id(v: any): any, जो input और return types के बीच connection खो देता है।
  2. यह भूल जाना कि T सिर्फ एक placeholder है, और T.length या new T() लिखना जैसे यह एक असली value हो।
  3. explicit type arguments pass करना जो argument से match न करें, जैसे identity<string>(42), जो एक error है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.