Reflection
The concept of decorating your code is tightly coupled with a concept called reflection. In a nutshell, reflection is the capability of a certain piece of code to examine and be introspective about itself – in a sense, to do some navel-gazing. It means that a piece of code can have access to things such as the variables, functions, and classes defined inside it. Most languages provide us with some kind of reflection API that enables us to treat the code itself as if it was data, and since TypeScript is built upon JavaScript, it inherits the JavaScript reflection capabilities.
JavaScript does not have an extensive reflection API, but there is a proposal (https://tc39.es/ecma262/#sec-reflection) to add proper metadata (data about data) support to the language.
Setting Up Compiler Options
TypeScript's decorators use the aforementioned proposed feature, and in order to use them, you have to enable the TypeScript compiler (tsc
) accordingly. As covered in...