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

Parameter के Decorators

Parameter decorators TypeScript के modern standard decorator model के बजाय legacy experimental decorator system से belong करते हैं। वे method या constructor parameters observe कर सकते हैं, लेकिन वे एक parameter को directly replace या इसकी value change नहीं कर सकते।
Syntax
typescript
function DecoratorName(target: any, key: string, index: number) {
  // inspect the parameter
}

class ClassName {
  method(@DecoratorName param: type) {}
}

Legacy Parameter Decorator Signature

Legacy (experimentalDecorators) parameter decorator signature target object, वह method name जिससे parameter belongs करता है, और उस method के argument list के अंदर parameter का numeric index receive करता है।

उदाहरण: Legacy Parameter Decorator Signature

typescript
function logParam(target: any, methodName: string, index: number) {
  console.log(`Parameter ${index} of ${methodName} decorated`);
}
class Service {
  greet(@logParam name: string) {
    return "Hello " + name;
  }
}
new Service().greet("Ravi");

Parameter Indexes

चूंकि parameter decorators को सिर्फ एक name के बजाय एक index मिलता है, उन पर built tooling (जैसे dependency-injection frameworks) को आमतौर पर उस index का सेंस बनाने के लिए reflection के through method की declared parameter types भी पढ़नी पड़ती हैं।

उदाहरण: Parameter Indexes

typescript
function inject(target: any, methodName: string, index: number) {
  console.log(`Injecting into parameter index ${index}`);
}
class Controller {
  handle(@inject req: any, res: any) {
    return "handled";
  }
}
new Controller().handle({}, {});

Parameter Metadata Record करना

एक common उपयोग यह record करना है कि कौन सा parameter index एक injected value receive करे एक metadata map में, जिसे code का कोई और part (आमतौर पर method decorator) बाद में तब पढ़ता है जब method actually invoke होता है।

उदाहरण: Recording Parameter Metadata

typescript
const injectionMap = new Map<string, number[]>();
function injectable(target: any, methodName: string, index: number) {
  const existing = injectionMap.get(methodName) ?? [];
  existing.push(index);
  injectionMap.set(methodName, existing);
}
class Repo {
  find(@injectable id: number) {
    return id;
  }
}
new Repo();
console.log(injectionMap);

Parameter Decorator Limitations

Parameter decorators यह change नहीं कर सकते कि क्या value pass होती है या directly call intercept नहीं कर सकते — वे सिर्फ parameter के साथ metadata attach कर सकते हैं, तो कोई भी असली behavior change किसी paired method या class decorator में होना चाहिए।

उदाहरण: Parameter Decorator Limitations

typescript
function noteOnly(target: any, methodName: string, index: number) {
  // Cannot change the value passed in -- only records metadata.
  console.log(`Noted parameter ${index}, value unaffected`);
}
class Logger {
  log(@noteOnly message: string) {
    console.log(message);
  }
}
new Logger().log("hello");

Modern Decorator Alternative

TypeScript का newer standard decorators proposal parameter decorators को पूरी तरह drop करता है validation logic को directly method decorators के अंदर attach करने के favor में, तो legacy parameter decorators ज़्यादातर older dependency-injection codebases में दिखते हैं।

उदाहरण: Modern Decorator Alternative

typescript
// Legacy: parameter decorators (experimentalDecorators)
// Modern: validation moves into the method decorator itself
function validateArgs(target: any, context: ClassMethodDecoratorContext) {
  return function (this: any, ...args: any[]) {
    console.log("Validating all args together:", args);
    return target.call(this, ...args);
  };
}
class Service {
  @validateArgs
  process(id: number) {
    return id;
  }
}
new Service().process(1);
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. experimentalDecorators के बिना parameter decorators उपयोग करना, क्योंकि standard decorators इन्हें support नहीं करते।
  2. यह उम्मीद करना कि decorator argument value change करेगा, जब इसे सिर्फ index मिलता है और यह metadata attach कर सकता है।
  3. यह मानना कि parameter का name available है, जब सिर्फ index number pass होता है।
🔒

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.