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

Decorators का परिचय

Decorators वे functions हैं जो TypeScript में classes और class members को observe या modify कर सकते हैं। Modern TypeScript classes, methods, getters, setters, fields, और auto-accessors के लिए standard decorators support करता है।
Syntax
typescript
function decoratorName(target: any, context: ClassMethodDecoratorContext) {
  // wrap or inspect the decorated member
}

class ClassName {
  @decoratorName
  methodName() { }
}

Decorator क्या है?

एक decorator एक function है जो @ syntax से directly एक class, method, property, या parameter के ऊपर apply होता है; यह उस declaration के बारे में information receive करता है जिससे यह attached है और इसे observe या modify कर सकता है।

उदाहरण: What Is a Decorator?

typescript
function logged(target: any, context: ClassMethodDecoratorContext) {
  console.log(`Decorator applied to ${String(context.name)}`);
}
class Greeter {
  @logged
  greet() {
    return "hi";
  }
}
new Greeter().greet();

Decorator Syntax

Decorators उस declaration से immediately पहले लिखे जाते हैं जिसे वे decorate करते हैं, और एक ही declaration पर कई decorators stack किए जा सकते हैं, एक specific, well-defined order में apply होते हुए।

उदाहरण: Decorator Syntax

typescript
function first(target: any, ctx: ClassMethodDecoratorContext) {
  console.log("first decorator on", String(ctx.name));
}
function second(target: any, ctx: ClassMethodDecoratorContext) {
  console.log("second decorator on", String(ctx.name));
}
class Example {
  @first
  @second
  run() {}
}
new Example().run();

Decorator Context

Standard decorators एक context object receive करते हैं जो describe करता है कि क्या decorate किया जा रहा है — इसका kind, name, और दूसरी metadata — जिसे decorator यह decide करने के लिए उपयोग कर सकता है कि कैसे behave करे।

उदाहरण: Decorator Context

typescript
function describe(target: any, context: ClassMethodDecoratorContext) {
  console.log("Decorating", context.kind, String(context.name));
}
class Service {
  @describe
  start() {}
}
new Service();

Decorated Values Replace करना

कुछ decorators एक compatible replacement value return कर सकते हैं; method decorators, उदाहरण के लिए, आमतौर पर original method का एक wrapped version return करते हैं जो call के around logging जैसा behavior जोड़ता है।

उदाहरण: Replacing Decorated Values

typescript
function logCall(target: any, context: ClassMethodDecoratorContext) {
  return function (this: any, ...args: any[]) {
    console.log("Calling", String(context.name));
    return target.call(this, ...args);
  };
}
class Calculator {
  @logCall
  add(a: number, b: number) {
    return a + b;
  }
}
console.log(new Calculator().add(2, 3));

Modern Decorators Enable करना

Current TypeScript में, standard decorators legacy experimentalDecorators compiler flag की ज़रूरत के बिना काम करते हैं, older, non-standard implementation के बजाय newer, TC39-aligned decorators proposal follow करते हुए।

उदाहरण: Enabling Modern Decorators

typescript
// Modern (TC39) decorators work without experimentalDecorators:
function marker(target: any, context: ClassMethodDecoratorContext) {}
class Widget {
  @marker
  render() {
    return "rendered";
  }
}
console.log(new Widget().render());
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. सही compiler setup के बिना decorators उपयोग करना, क्योंकि legacy decorators को experimentalDecorators चाहिए जबकि standard वाले newer signature expect करते हैं।
  2. standard decorators के under old (target, key, descriptor) signature के साथ एक decorator लिखना, जहां यह (value, context) receive करता है।
  3. यह मानना कि @logged हर बार method call होने पर run होता है, जब decorator function class define होते समय एक बार run होता है।
🔒

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.