-
Book Overview & Buying
-
Table Of Contents
Mastering TypeScript - Fourth Edition
By :
In this section of the chapter, we will work through each of the different types of decorators and experiment with the information that is provided by each decorator function. We will then use what we have learned in order to provide examples of some practical applications of decorators.
We know that in order to define a class decorator, we must define a function that has a single parameter, which is of type Function. Let's take a closer look at this parameter, as follows:
function classConstructorDec(constructor: Function) {
console.log(`constructor : ${constructor}`);
}
@classConstructorDec
class ClassWithConstructor {
constructor(id: number) { }
}
Here, we have a decorator function named classConstructorDec, which is logging the value of the constructor argument to the console. We have then applied this decorator to a class named ClassWithConstructor. This ClassWithConstructor class has a single constructor function...