Property के Decorators
In this page:
function DecoratorName(target: any, key: string) {
// inspect the property
}
class ClassName {
@DecoratorName
property: type;
}
Basic Field Decorator
एक field decorator एक बार run होता है जब class field defined होता है और किसी भी instance create होने से पहले field की initial value पढ़ या transform कर सकता है, आपको यह control देते हुए कि यह property कैसे शुरू होती है।
उदाहरण: Basic Field Decorator
function logged(value: undefined, context: ClassFieldDecoratorContext) {
console.log(`Field ${String(context.name)} is being defined`);
}
class User {
@logged
name = "Ravi";
}
new User();
Initial Values Transform करना
चूंकि decorator field के initializer function को देखता है, यह starting value को normalize या validate करने के लिए इसे wrap कर सकता है, जैसे एक string trim करना या एक number clamp करना, property assign होने से पहले।
उदाहरण: Transforming Initial Values
function trimmed(value: undefined, context: ClassFieldDecoratorContext) {
return function (this: any, initial: string) {
return initial.trim();
};
}
class Form {
@trimmed
username = " Ravi ";
}
console.log(new Form().username);
Field Decorator Initializers
Decorator को class की field initializer argument के रूप में मिलती है, तो एक नया initializer function return करना आपको field की default value कैसे compute होती है इसे पूरी तरह swap out करने देता है।
उदाहरण: Field Decorator Initializers
function withDefault(defaultValue: number) {
return function (value: undefined, context: ClassFieldDecoratorContext) {
return function (this: any, initial: number) {
return initial ?? defaultValue;
};
};
}
class Settings {
@withDefault(10)
volume = 10;
}
console.log(new Settings().volume);
Field Decorator Factories
एक field decorator factory उपयोग के point पर arguments accept करती है, तो एक ही decorator हर field के लिए एक अलग rule enforce कर सकता है, जैसे एक string field के लिए minimum length और दूसरे के लिए एक अलग।
उदाहरण: Field Decorator Factories
function minLength(min: number) {
return function (value: undefined, context: ClassFieldDecoratorContext) {
return function (this: any, initial: string) {
if (initial.length < min) throw new Error(`Too short, need ${min}`);
return initial;
};
};
}
class Account {
@minLength(3)
username = "Ravi";
}
console.log(new Account().username);
Static and Instance Fields
TypeScript की decorator metadata static fields (class से ही attached) और instance fields (हर object से attached) के बीच अंतर करती है, और shared state मानने से पहले एक decorator को check करना होता है कि यह किस kind decorate कर रहा है।
उदाहरण: Static and Instance Fields
function describe(value: undefined, context: ClassFieldDecoratorContext) {
console.log(`${String(context.name)} is ${context.static ? "static" : "instance"}`);
}
class Counter {
@describe
static total = 0;
@describe
count = 0;
}
new Counter();
- यह उम्मीद करना कि एक field decorator को directly field value मिलेगी, जब standard decorators को
undefinedमिलता है और इन्हें एक initializer function return करना चाहिए। - यह भूल जाना कि initializer हर instance के लिए run होता है, तो इसके अंदर shared mutable state misleading है।
- एक method पर या इसके विपरीत एक field decorator उपयोग करना, क्योंकि हर decorator type एक अलग context लेता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: