- Mastering TypeScript 3
- Nathan Rozentals
- 325字
- 2021-07-02 12:42:50
Interfaces, Classes, and Inheritance
We have already seen how TypeScript uses basic types, inferred types, function signatures and tuples to bring a strongly typed development experience to JavaScript. This strongly typed paradigm also includes object-oriented features that are similar to other languages, such as interfaces, classes, and inheritance. Where TypeScript has always had language constructs for interfaces and classes, these object-oriented features have only recently been ratified with the ECMAScript six standard. This means that, in order to use these object-oriented features in JavaScript, our target environment must support the ES6 standard. TypeScript, however, will take care of generating ES3 or ES5-compatible JavaScript. This means that, no matter what JavaScript runtime we are targeting, we are able to use all of the goodness of object-oriented TypeScript to design and build our applications. In this chapter, we will look at these object-oriented concepts, how they are used in TypeScript, and what benefits they bring to the JavaScript development experience.
This chapter is broken up into two main sections. The first section of this chapter is intended for readers who are using TypeScript for the first time, and covers interfaces, classes, and inheritance from the ground up. The second section of this chapter builds on this knowledge, and shows how to create and use classes, interfaces, and inheritance by building a sample Factory Design pattern.
If you have experience with TypeScript, are actively using interfaces and classes, and understand inheritance, then by all means skim through this chapter. You may be more interested in some of the keywords that TypeScript provides to compare the structure of classes, such as weak types, the instanceof operator, or the in operator. The later sections of the chapter discuss the Factory Design Pattern, and abstract classes.
This chapter will cover the following topics:
- Interfaces
- Weak types
- The in operator
- Classes
- Class constructors
- Class modifiers
- Static functions and properties
- Inheritance
- Abstract classes
- JavaScript closures
- Instanceof and type guards
- The Factory Design Pattern