14 Using decorators
This chapter covers
- Defining and applying decorators
- Decorating classes, methods, properties, accessors, and auto-accessors
- Using decorator context data
- Creating decorators with a factory function
- Accumulating state data in decorators
Decorators are a forthcoming addition to the JavaScript language that transform features defined by classes. TypeScript has long supported an experimental version of decorators, used mainly in Angular development, but TypeScript 5 has added support for the version of decorators that will be adopted in a future release of the JavaScript specification. Table 14.1 summarizes the chapter.
Table 14.1. Chapter summary
| Problem |
Solution |
Listing |
| Transform a class feature |
Define and apply a decorator |
9-12, 16-30, 38-41 |
| Get details of the feature to be transformed |
Use the decorator context object |
13-15 |
| Configure each application of a decorator |
Use a factory function |
31-37 |
| Perform initial setup for a decorator |
Use an initializer function |
42-44 |
| Accumulate state data |
Define a variable outside of the decorator function or factory function |
45-46 |
For quick reference, table 14.2 lists the TypeScript compiler options used in this chapter.