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 (view table figure)

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.

14.1 Preparing for this chapter

 
 
 

14.2 Understanding decorators

 
 

14.2.1 Using decorator context data

 

14.2.2 Using specific types in a decorator

 
 
 

14.3 Using the other decorator types

 
 
 
 

14.3.1 Creating a class decorator

 
 

14.3.2 Creating a field decorator

 
 
 

14.3.3 Creating an accessor decorator

 
 

14.3.4 Creating an auto-accessor decorator

 
 

14.4 Passing an additional argument to a decorator

 
 
 

14.5 Applying multiple decorators

 

14.6 Using an initializer

 
 
 
 

14.7 Accumulating state data

 
 
 
 

Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest