← Back to TypeScript Course | Chapter 10: Decorators | Lesson 2 of 7

Class के Decorators

एक class decorator तब run होता है जब एक class defined होती है और class constructor plus एक ClassDecoratorContext receive करता है। यह class को register कर सकता है, static initialization logic run कर सकता है, या एक compatible replacement class return कर सकता है।
Syntax
typescript
function DecoratorName(constructor: Function) {
  // modify or inspect the class
}

@DecoratorName
class ClassName {}

Basic Class Decorator

एक class decorator एक class declaration से directly पहले रखा जाता है और पूरी class को inspect या transform कर सकता है, class के constructor को इसके argument के रूप में receive करते हुए।

उदाहरण: Basic Class Decorator

typescript
function sealed(constructor: Function) {
  console.log("Sealing class", constructor.name);
}
@sealed
class Account {
  balance = 0;
}
new Account();

Class Initializers

Class decorators class body को directly modify किए बिना, class defined होने के ठीक बाद run होने के लिए additional logic schedule करने के लिए context.addInitializer call कर सकते हैं।

उदाहरण: Class Initializers

typescript
function withInit(target: any, context: ClassDecoratorContext) {
  context.addInitializer(function (this: any) {
    console.log("Initialized", this.constructor.name);
  });
}
@withInit
class Service {}
new Service();

एक Class Replace करना

एक class decorator original class के compatible एक नया constructor return कर सकता है, effectively जहां भी इसे उपयोग किया जाता है वहां एक modified या wrapped version swap in करते हुए — cross-cutting behavior जोड़ने के लिए एक common pattern।

उदाहरण: Replacing a Class

typescript
function loggedClass<T extends { new (...args: any[]): {} }>(Base: T) {
  return class extends Base {
    constructor(...args: any[]) {
      super(...args);
      console.log("Created instance of", Base.name);
    }
  };
}
@loggedClass
class Widget {}
new Widget();

Class Decorator Factories

एक decorator factory एक function है जो खुद एक class decorator return करता है, इसे अपने arguments से configuration values capture करने देते हुए तो एक factory differently-configured decorators produce कर सकती है।

उदाहरण: Class Decorator Factories

typescript
function tag(name: string) {
  return function (target: Function) {
    console.log(`Tagging ${target.name} as ${name}`);
  };
}
@tag("service")
class UserService {}
new UserService();

Multiple Class Decorators

एक ही class पर कई class decorators एक साथ apply किए जा सकते हैं; जब कई हों, तो decorator expressions एक specific, predictable order में evaluate और apply होते हैं।

उदाहरण: Multiple Class Decorators

typescript
function first(target: Function) {
  console.log("first applied to", target.name);
}
function second(target: Function) {
  console.log("second applied to", target.name);
}
@first
@second
class Example {}
new Example();
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. एक plain decorator पर parentheses के साथ @sealed() लिखना, जब call सिर्फ factories के लिए है।
  2. यह भूल जाना कि एक class decorator class receive करता है, एक instance नहीं, तो this properties अभी वहां नहीं हैं।
  3. एक replacement class return करना जो original को extend नहीं करती, जो type compatibility तोड़ता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

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