- Mastering TypeScript 3
- Nathan Rozentals
- 259字
- 2021-07-02 12:42:56
Decorators, Generics, and Asynchronous Features
Above and beyond the concepts of classes, interfaces, and inheritance, the TypeScript language introduces a number of advanced language features in order to aid the development of robust object-oriented code. These features include decorators, generics, promises, and the use of the async and await keywords when working with asynchronous functions. Decorators allow for the injection and querying of metadata when working with class definitions, as well as the ability to programmatically attach to the act of defining a class. Generics provide a technique for writing routines where the exact type of an object used is not known until runtime. Promises provide the ability to write asynchronous code in a fluent manner, and async await functions will pause execution until an asynchronous function has completed.
When writing large-scale JavaScript applications, these language features become part of the programmers' toolbox and allow for the application of many design patterns within JavaScript code.
This chapter is broken into three parts. The first part covers decorators, the second part covers generics, and the third part deals with asynchronous programming techniques using promises and async await. In earlier versions of TypeScript, promises and async await could only be used in ECMAScript 6 and above, but this functionality has now been extended to include ECMAScript 3 targets.
We will cover the following topics in this chapter:
- Decorator syntax
- Decorator factories
- Class, method, and parameter decorators
- Decorator metadata
- Generic syntax
- Using and constraining the type of T
- Generic interfaces
- Promises and promise syntax
- Using promises
- async and await
- Handling await errors