Object-oriented has its own set of jargon, and TypeScript relies on much of it. In this chapter, all the concepts of object-oriented that TypeScript supports are discussed with examples. We will see what a class is and how to instantiate a class into an object. We will also see how a constructor can be strongly typed with TypeScript, and how, with a shorthand syntax, we can assign a class's fields directly from the constructor. We will cover the principle of encapsulation with visibility, how to implement an interface, and how to bring abstraction to a class.
In this chapter, we will cover the following topics:
- What is a class and how do we define one?
- How type comes into play with a class's constructor
- What is encapsulation using public, private, and protected
- Reducing the definition of a field with a value set at construction time
- What is static?
- Use cases for a non-public constructor
- Using an object from a class versus an object literal
- How an interface can be useful in object-oriented
- Bringing abstraction with an abstract class
- How to have a property that is read-only
- Enforcing a specific constructor from an interface