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

Generic Classes की जानकारी

Generic classes reusable classes बनाने के लिए type parameters उपयोग करती हैं जो अलग-अलग data types के साथ काम करती हैं। Class अपनी methods द्वारा stored और returned values की type preserve कर सकती है।
Syntax
typescript
class ClassName<T> {
  property: T;
  constructor(value: T) {
    this.property = value;
  }
}

Basic Generic Class

एक class एक type parameter declare कर सकती है और इसे अपनी properties और methods के लिए उपयोग कर सकती है, इसलिए वही class definition instantiate होने पर supply की गई किसी भी type के लिए specialized objects produce करती है।

उदाहरण: Basic Generic Class

typescript
class Box<T> {
  constructor(public value: T) {}
}
const box = new Box<string>("hello");
console.log(box.value);

Generic Stack

एक generic class stack या queue जैसी reusable data structures implement कर सकती है जबकि अंदर stored items की exact type preserve करते हुए, any में fall back करने और हर read पर type safety खोने के बजाय।

उदाहरण: Generic Stack

typescript
class Stack<T> {
  private items: T[] = [];
  push(item: T) { this.items.push(item); }
  pop(): T | undefined { return this.items.pop(); }
}
const stack = new Stack<number>();
stack.push(1);
stack.push(2);
console.log(stack.pop());

Generic Class Methods

एक generic class के अंदर methods class के अपने type parameter को consistent input और output typing के लिए उपयोग कर सकते हैं, इसलिए एक generic Stack<T> पर push और pop जैसे operations हमेशा सहमत होते हैं कि T क्या है।

उदाहरण: Generic Class Methods

typescript
class Stack<T> {
  private items: T[] = [];
  push(item: T): void { this.items.push(item); }
  peek(): T { return this.items[this.items.length - 1]; }
}
const s = new Stack<string>();
s.push("top");
console.log(s.peek());

Objects के साथ Generic Classes

Generic classes एक type alias या interface को type argument के रूप में उपयोग करके complex object types store कर सकती हैं, जो exactly वही तरीके से काम करता है जैसे number या string जैसी एक simple type supply करना।

उदाहरण: Generic Classes with Objects

typescript
interface User { name: string; }
class Repository<T> {
  private items: T[] = [];
  add(item: T) { this.items.push(item); }
  all(): T[] { return this.items; }
}
const users = new Repository<User>();
users.add({ name: "Farhan" });
console.log(users.all());

Generic Classes में Type Inference

TypeScript अक्सर new को pass किए गए constructor arguments से सीधे एक class का type parameter infer कर सकता है, इसलिए callers को हमेशा type argument explicitly लिखना नहीं पड़ता।

उदाहरण: Type Inference in Generic Classes

typescript
class Box<T> {
  constructor(public value: T) {}
}
const box = new Box(123); // T inferred as number
console.log(box.value);
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. type argument भूल जाना, जैसे new Stack(), इसलिए type unknown बन जाता है या बहुत loosely infer होता है।
  2. class के type parameter को एक static member में उपयोग करना, जैसे static create(x: T), जब statics T को reference नहीं कर सकते।
  3. एक empty Stack<T> से pop करना और बिना check किए result उपयोग करना, क्योंकि pop() T | undefined return करता है।
🔒

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.