Parameter के Decorators
In this page:
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
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
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
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
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
// 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);
experimentalDecoratorsके बिना parameter decorators उपयोग करना, क्योंकि standard decorators इन्हें support नहीं करते।- यह उम्मीद करना कि decorator argument value change करेगा, जब इसे सिर्फ index मिलता है और यह metadata attach कर सकता है।
- यह मानना कि parameter का name available है, जब सिर्फ
indexnumber pass होता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: