<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marwa Abdelaal</title>
    <description>The latest articles on DEV Community by Marwa Abdelaal (@marwaabdelaal).</description>
    <link>https://dev.to/marwaabdelaal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1055115%2F9f6c8516-c877-494a-96ab-afad18833dec.jpeg</url>
      <title>DEV Community: Marwa Abdelaal</title>
      <link>https://dev.to/marwaabdelaal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marwaabdelaal"/>
    <language>en</language>
    <item>
      <title>Decorators in typescript</title>
      <dc:creator>Marwa Abdelaal</dc:creator>
      <pubDate>Sat, 01 Apr 2023 21:00:47 +0000</pubDate>
      <link>https://dev.to/marwaabdelaal/decorators-in-typescript-2b2f</link>
      <guid>https://dev.to/marwaabdelaal/decorators-in-typescript-2b2f</guid>
      <description>&lt;p&gt;Typescript is a superset of Javascript. Typescript allows Javascript developers to add static type-checking and other modern features to plain Javascript. Some of these extra features do get added to the standard Javascript(ECMAScript).&lt;/p&gt;

&lt;p&gt;The concept of Decorators is one of those extra features. In this article, we would see what Decorators are and how they can be used in Typescript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Decorators?
&lt;/h2&gt;

&lt;p&gt;Decorators are a feature in TypeScript that allow you to modify the behavior of classes, methods, properties, and other members at design time. Decorators are implemented as functions that can be attached to declarations using the &lt;code&gt;@&lt;/code&gt; symbol.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Decorators used for?
&lt;/h2&gt;

&lt;p&gt;They can be used for a wide range of tasks, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding additional behavior to a class or its members.&lt;/li&gt;
&lt;li&gt;Implementing dependency injection.&lt;/li&gt;
&lt;li&gt;Modifying third-party libraries without having to change the implementation of the library itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;To use decorators, you have to set the &lt;code&gt;experimentalDecorators&lt;/code&gt; compiler to &lt;code&gt;true&lt;/code&gt; option in the &lt;code&gt;tsconfig.json&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;compilerOptions&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;target&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ES5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;experimentalDecorators&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After setting up your project to use decorators, let's get coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Decorators
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Class Decorators
&lt;/h3&gt;

&lt;p&gt;Class decorators are used to modify the behavior of a class at design time. They can be applied to the class declaration itself, and can also be used to modify the constructor function of the class.&lt;/p&gt;

&lt;p&gt;Here is an example of a class decorator that adds a new method to a class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;addMethod&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Greeter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greeter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Greeter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;greeter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs "Hello, world!"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a class decorator called &lt;code&gt;addMethod&lt;/code&gt; that takes one argument, the target object (which is the constructor function of the class). The decorator adds a new method called &lt;code&gt;greet&lt;/code&gt; to the prototype of the target object.&lt;/p&gt;

&lt;p&gt;We then apply the &lt;code&gt;addMethod&lt;/code&gt; decorator to the &lt;code&gt;Greeter&lt;/code&gt; class using the &lt;code&gt;@&lt;/code&gt; symbol. When we create a new instance of &lt;code&gt;Greeter&lt;/code&gt; and call the &lt;code&gt;greet&lt;/code&gt; method, the decorator intercepts the call and executes the new method that we added to the class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Property Decorators
&lt;/h2&gt;

&lt;p&gt;Property decorators are used to modify the behavior of a class property at design time. They can be applied to the property declaration itself, and can also be used to modify the descriptor object that describes the property's properties.&lt;/p&gt;

&lt;p&gt;The property decorator receives two arguments.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The prototype of the class for an instance member OR the constructor function of the class for a static member.&lt;/li&gt;
&lt;li&gt;The name of the property.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example of a property decorator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;propertyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defineProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;propertyKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;readonly&lt;/span&gt;
  &lt;span class="nx"&gt;myProperty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myProperty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;new value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// throws a TypeError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a property decorator called &lt;code&gt;readonly&lt;/code&gt; that sets the &lt;code&gt;writable&lt;/code&gt; property of the target property descriptor to &lt;code&gt;false&lt;/code&gt;. We then apply the decorator to the &lt;code&gt;myProperty&lt;/code&gt; property of the &lt;code&gt;MyClass&lt;/code&gt; class using the &lt;code&gt;@&lt;/code&gt; symbol. Also Property decorators can be used to override the property being decorated. This can be done with the static method &lt;code&gt;Object.defineProperty&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Decorators are just a clean syntax for wrapping a piece of code with a function&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Are there any downsides?
&lt;/h2&gt;

&lt;p&gt;While decorators are a powerful tool for adding functionality to classes, properties, and methods in TypeScript, there are some downsides to consider:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Increased Complexity&lt;/strong&gt;: Decorators can make the code more complex, especially if decorators are heavily used or if there are many decorators applied to a single property or method. This can make it harder to understand the behavior of the code, especially for developers who are not familiar with the decorator syntax.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Debugging&lt;/strong&gt;: Debugging code that uses decorators can be more difficult because the decorator code is executed at runtime, which can make it harder to trace the flow of execution and find errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limited Browser Support&lt;/strong&gt;: While TypeScript supports decorators, they are not natively supported by all browsers. If you are targeting a browser that does not support decorators, you will need to use a transpiler like Babel to convert the decorator syntax to code that is compatible with that browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: Using decorators can impact the performance of the application, especially if many decorators are used. The extra overhead of decorator code can slow down the execution of the application, especially on slower devices or when dealing with large amounts of data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;It's important to consider these downsides when deciding whether to use decorators in your TypeScript code. While decorators can be a useful tool for adding functionality to your code, they should be used judiciously and only when the benefits outweigh the costs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;At this point I hope you had a good understanding of decorators, how they work and when to use them or avoid using them. In this article I covered only two types of decorators, however, there’s much more to learn. Decorators in Typescript can be applied not only to classes and methods, but also to properties of classes, and method arguments. I hope that with your newly acquired understanding of decorators you’ll be able to pick it up easily from the &lt;a href="https://www.typescriptlang.org/docs/handbook/decorators.html"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you’ve enjoyed this article feel free to clap for it so more people will find it and enjoy it as well, and if you wish to see more of the stuff I write you’re welcome to follow me. Let me know in the comments if you have any questions.&lt;br&gt;
Thanks for reading!&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>decorators</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
