Decorators are awesome features, Many libraries are made out of decorators like react and angular. These are an awesome concept
In this article we are going to learn about typescript decorators.
What are TypeScript Decorators?
Decorators are basically just functions at their core.
With decorators you can apply re-useable behaviors to classes, methods, properties. Decorators are inspired by decorators in other languages like Java and Python
Dead Simple Chat offers Javascript Chat API and SDK to add in-app chat to your React applications in minutes. Dead Simple Chat is a highly customizable chat solution and can be used for any chat use case.
Types of Decorators
These Decorators can apply to
- Class
- Method
- Class Property
- Accessor and
- Method Parameter
- Class decorator
Class decorator is applied to the constructor of a class and can be used to observe, modify and change a class definition
example of a class decorators
type ClassDecorator = <TFunction extends Function>
(target: TFunction) => TFunction | void;
- @Params
target
The constructor of the class
@Returns
If the decorator returns a value. It replaces the class definition and it is a way to extend class definition with new properties and methods
Let us look at this with the help of an example
Here TechClass
is a class decorator when the is created the decorator is triggered and the class name that is CoolClass
is logged to the console
2. Method Decorators
Method decorators are used for methods, they can change and replace or observe a functions definition
Let us learn more about method decorators with an example:
Here the techfunc
decorator logs the functions name to the console
. the TestFunc
method of the CoolClass
triggers the TechFunc
decorator which in turn logs the function name to the console
let us look at another example
Here we have a CoolLogs
function that serves as a decorator for the class named c
Our decorator takes 3 parameters
target
propertyKey
descriptor
Inside the const
original the original value of the function is stored.
then the original function is replaced by another function that takes a number of arguments
then it calls the new function with the same arguments
Property Decorators
Property descriptors can be used to change or observer a properties property definition
Dead Simple Chat offers Javascript Chat API and SDK to add in-app chat to your React applications in minutes. Dead Simple Chat is a highly customizable chat solution and can be used for any chat use case.
for example
Here the TestProperty
defines the TestProperty
decorator that observes a ClassProperty
with the type string
whenever a new class instance is created the Test property logs the name of the property to the console
Parameter Decorator
Parameter decorators are used to watch and change the functional or methods parameters
let us look at the parameter decorator with an example
Here we have a function TestParameter
that console.logs
the CoolParameter
parameter
and then we have SomeClass
with a method named exampleMethod
. When the exampleMethod
is called the decorator is triggered and the TestParameter
function is called which then logs the CoolParameter
to the console
Accessor Decorators
Accessor decorators are like method decorators but the only difference is the that the Method decorators have keys in their descriptor like
- Value
- writable
- configurable
- enumerable
The descriptor in the Accessor decorators has keys
- get
- set
- enumerable
- configurable
The accessor decorators accept the follow theee parameters
- Constructor function or target prototype
- property name
- property descriptor
Dead Simple Chat offers Javascript Chat API and SDK to add in-app chat to your React applications in minutes. Dead Simple Chat is a highly customizable chat solution and can be used for any chat use case.
When to use Decorators
- Before/After Hooks
- Watch property changes and method calls.
- Transform parameters
- Add extra method or properties
- Runtime type validation
- Auto serialization and deserialization
- Dependency Injection
1. Before /After Hooks
Before / After hooks means the decorator is called before and after the function is called which is quite useful in debugging, logging, and measuring performance
Let us look into it through an example
Here we have the ExampleOfBeforeAfterHookFunction
function
where we are storing the originalMethod
in the orignalMethod
const
Then we replace the original
method with a new function implement that takes n number of arguments using the ...args
property
then we log a before property name and then we call the originalMethod
function and then we log the after property name.
This is how you can implement the before / after hooks method
2. Watch property changes and method calls.
Watch is a property decorator that logs the property as well as the method calls of a decorated method
This is especially useful when debugging and performance monitering.
Let us learn more about Watch property and how it works using an example
3. Transform Parameters
In transform parameters function applies the transformation function to a specific parameter before the method is called
Let us learn more about transform parameters with the help of an example
What is happening here
The method decorator see logs the names and arguments of the decorated method
When the method see is called in the instance of MyClass it logs the method name, and sets the property value of myproperty
4. Add extra method or property
You can add an extra method or property using the decorators. Here is how you can achieve this using an example
the code has a function called AddMethod
which adds a function ot the prototype of the decorated class which in this case is SomeClass
When an instance of SomeClass
is called the addMethod
can be called on it. The add method logs a message to the console
5. Runtime Type Validation
Runtime type validation is ensuring the type of values of the parameters and variables and const and data used in the function during run time matches the values of the expected type
unlike static type validation which happens during software compilation the run time type validation happens when the function or programing is running thus it is helpful in validating external input data
Let us learn more about Runtime Type Validation using an example
6. Auto serialization and deserialization
Auto serialization and deserialization refers to the practice of converting from one data type to another data type deserialization in order to store the data in a particular format
then converting again from one data type to the orignal data type for the purpose of rendering the data or other function can consume the data
Here is an example explaining how decorators can be used to do this
7. Dependency Injection
Decorators can also be used for dependency injection. Dependency injection is the practice of providing the functions and Objects that a method depends upon to it as parameter
this helps in decoupling the code and makes it cleaner
Decorators helps in dependency injection by providing a way to inject dependencies directly into class constructors without changing class implementation
This making them easier to maintain and test
Dead Simple Chat offers Javascript Chat API and SDK to add in-app chat to your React applications in minutes. Dead Simple Chat is a highly customizable chat solution and can be used for any chat use case.
Advantages of using decorators
- Cross Cutting concerns
When parts of a program relay on many other parts of the program. Like a program might relay on other classes methods or properties then this could be a use case for decorators and they provide an easy way to maintain the code and provide improved readability
2. Dependency Injection
Dependency injection is a software design pattern where an object or a function that relies on other objects or functions receives then
This is like an inversion of control and results in loosely coupled program and a code base that is modular
Here also decorators can in useful
3. Validation
Decorators can be used to validate inputs or the state of an Object.
4. Code organization
The code that doesn't belong in the main function or logic can be encapsulated using decorators thus making the code more readable and maintainable
Dead Simple Chat offers Javascript Chat API and SDK to add in-app chat to your React applications in minutes. Dead Simple Chat is a highly customizable chat solution and can be used for any chat use case.
You might be interested in some of our other articles
- Type Casting with TypeScript: A tutorial
- Pure Component in React
- Higher Order Components in React: Examples with Common Use-case Patterns
- React useReducer and Reducer: Guide with Examples
Conclusion
In this article we have learnt that how to use typescript decorators and we also have used a number of examples to make them easier to understand
I hope you find this article useful
Thanks for reading the article