The course is part of this learning path
This module introduces you to Classes and how to use them in TypeScript.
The objectives of this module are to provide you with and understanding of:
- What classes are
- How to use inheritance in TypeScript Classes
- How to use Access Modifiers
- How to use Abstracts
- How to use Structural Typing
This learning path is aimed at all who wish to learn how to use TypeScript
We welcome all feedback and suggestions - please contact us at email@example.com to let us know what you think.
TypeScript is a structural type system. If the types of all members are compatible, then the types are compatible, this except for private and protected members. It's also known as duck-typing, coming from the phrase, if it waddles like a duck and quacks like a duck, then it's a duck. And we'll demonstrate what we mean by this through the code. We declare a class STVehicle that has public properties, wheels and power. The class STCar extends STVehicle and called super in its constructor. A third class STTaxi is standalone and has public properties, wheels and power. On line 28 and below, we declare some instances of the car, taxi and vehicle. Through inheritance it's possible and logical that we can make my STVehicle, an instance of an STCar class. It's just how the, is a part of inheritance works. But what about an STTaxi? Could we set our instance of the STVehicle class to an instance of the STTaxi class? Yes, check line 41 on the top of the right to the screen. According to the structural typing rules, two objects are considered to be the same type, if both share the same set of properties, the STTaxi class is defined with the same properties and access modifiers as the STVehicle class. If it waddles like a duck and quacks like a duck, then it's a duck. We define another class on line 46. That's the same property names as the STVehicle class, but the typing and the access modifiers a slightly different. We create an instance of the Rikshaw class and try and set it to the value for the instance of the STVehicle class. And there's an error because the Rikshaw type is not assignable, to the STVehicle type. But what would happen if we compiled ignoring this? Now remember the only lesson we need, my STVehicle would log out as the ritual. So in this module, we've examined how classes are implemented in TypeScript paying particular attention to access modifies, including static, getters, and setters, abstract classes and functions, and how structural types apply to classes. It would be good to go and do some practice with the syntax and concepts now.