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

Property के Decorators

Modern TypeScript class fields customize करने के लिए field decorators उपयोग करता है। एक field decorator instances create होते समय initial field value receive करता है और एक initializer return कर सकता है जो उस value को transform करे।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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();
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. यह उम्मीद करना कि एक field decorator को directly field value मिलेगी, जब standard decorators को undefined मिलता है और इन्हें एक initializer function return करना चाहिए।
  2. यह भूल जाना कि initializer हर instance के लिए run होता है, तो इसके अंदर shared mutable state misleading है।
  3. एक method पर या इसके विपरीत एक field decorator उपयोग करना, क्योंकि हर decorator type एक अलग context लेता है।
🔒

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.