A practical guide to TypeScript decorators
What are decorators?
Although the definition might vary for different programming languages, the reason why decorators exist is pretty much the same across the board. In a nutshell, a decorator is a pattern in programming in which you wrap something to change its behavior.
Start by creating a blank Node.js project.
$ mkdir typescript-decorators
$ cd typescript decorators
$ npm init -y
Next, install TypeScript as a development dependency.
$ npm install -D typescript @types/node
The @types/node package contains the Node.js type definitions for TypeScript. We need this package to access some Node.js standard libraries.
Add an npm script in the package.json file to compile your TypeScript code.
TypeScript has labeled this feature as experimental. Nonetheless, it’s stable enough to use in production. In fact, the open source community has been using it for quite a while.
To activate the feature, you’ll need to make some adjustments to your tsconfig.json file.
Create a simple TypeScript file to test it out.
$ npm run build