-
Book Overview & Buying
-
Table Of Contents
The TypeScript Workshop
By :
A class decorator is a decorator function that is applied to the whole class. It can be used to observe, change, or replace wholesale a class definition. When a class decorator is called, it receives a single parameter – the constructor function of the calling class.
Property injection is one of the common scenarios that class decorations are used for. For example, let's say you're building a system that will model a school. You will have a class called Teacher that will have the properties and model the behavior of a teacher. The constructor for this class will take two parameters, an id number of the teacher, and the name of the teacher. This is how the class will look:
class Teacher {
constructor (public id: number, public name: string) {}
// other teacher specific code
}
Let's say we build the system and it's up and running. Everything is great, but after a...