Decorator is a quite good feature in Typescript even though it is currently still in an experimental state. But, because of its advantages, it is commonly used by any developer. Such a framework like Angular also heavily uses this feature from the beginning.
A decorator is a syntax for calling a function on the thing that it is decorated. It acts like a wrapper function or higher-order function that results in an improved thing or thing with additional things. The thing may be a class, class method, class property, or method property. The thing that will be decorated must have a specific type signature. A class decorator can only decorate a class, an object that has a constructor.
@mydecorator
class MyClass {}
The code above is similar to the following code.
class MyClass {}
const WrappedClass = mydecorator(MyClass);
We will take the example of utilizing a class decorator. First, we need to create a type of class constructor that passes a generic type for defining the shape of the class instance. The type can be generated by the new
operator which is applied to a function that returns a generic type value.
type Constructor<T> = new (...args: any[]) => T;
Then, we set up our decorator by creating a function that accepts a class with a valid type as the first parameter. A valid type means that it extends the class constructor. Besides that, it should have specific properties or methods that can define the shape of the specified class instance. For example, the class will have the getValue()
method that results in an object.
function withJson<T extends Constructor<{
getValue: () => { point: number }
}>>(Class: T) {
return class extends Class {
getJson = () => {
return JSON.stringify(this.getValue());
}
}
}
We can apply the function as a decorator for a class that aligns with the class constructor or in other words, it implements getValue()
method.
@withJson
class MyClass {
value = { point: 10 };
getValue() {
return this.value;
}
}
Then, we can create an instance of the class and call the getJson()
method.
const myObject = new MyClass();
console.log(myObject.getJson());
But, Typescript will show an error by the previous code. It is because the decorator won't change the shape or type structure that will be interpreted by Typescript. The getJson()
method won't be recognized as the valid property of the class instance. There are several solutions for this case. But, what more appropriate is by declaring a class which is defining the method as property and extending the declared class to the decorated class.
class Helper {
getJson: any;
}
@withJson
class MyClass extends Helper {
value = { point: 10 };
getValue() {
return this.value;
}
}
Last, how it is if we want to create a factory of a decorator that accepts any parameters for defining the resulting decorator. We can customize the withJson()
method into a factory. As an example, we will create a factory that accepts a parameter as an additional value for the point
property in the instance value.
const withJson = (additionalValue: number) => {
return <T extends Constructor<{
getValue: () => { point: number }
}>>(Class: T) => {
return class extends Class {
getJson = () => {
return JSON.stringify({ point: this.getValue().point + additionalValue});
}
}
};
}
@withJson(5)
class MyClass extends Helper {
//...
}
const myObject = new MyClass();
console.log(myObject.getJson());
So, if the code above is run, it will result in { "point": 15 }
.
Comments
Post a Comment