Object-Oriented Programming in TypeScript

I replicated Object-Oriented Programming in Go to TypeScript. You can read the explanation or theory of OOP in that article. I'm here to show you how I technically replicated the code.
Abstraction
We created a class as a template to define an object. Of course, a class or object has properties and methods.
Properties
1 2 3 4 5 |
|
We created a class called Animal, exported the class, and set a default or initial value for its properties. This class is on a file named Animal.ts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
We created a file named index.ts, imported the Animal class on it, defined two objects called cat and dog, and defined their properties.
Methods
1 2 3 4 5 6 7 |
|
We already have the properties. Let's create a method to print the properties. Add the print function to the Animal class.
1 2 |
|
In index.ts, we called the method.
Inheritance
1 2 3 4 5 |
|
Let's create a sub-class for the Animal that has abilities.
1 2 3 4 5 6 7 8 9 10 |
|
The dragon has a print method from its parent.
Polymorphism
1 2 3 4 5 6 7 8 |
|
Let's create another form of the print method for the Monster class that prints abilities. Put the print method to the Monster class and execute the index.ts. We will see a different result.
Encapsulation
Encapsulation is about Access Modifiers. Let me explain about Interface and Constructor first.
Interface
1 2 3 4 5 6 7 8 |
|
The interface allows us to define a must-defined method for classes that implements it. Here, we created getters and setters for name, feet, and hasPaws.
1 2 3 4 5 |
|
In the Animal.ts, we imported the interface of Properties and implemented it in the Animal class. Try to remove the property for an error.
Constructor
We created objects and defined their properties before. That is ugly. The constructor allows us to define properties along with the object.
1 2 3 4 5 |
|
The constructor of the Animal.
1 2 3 4 |
|
The constructor of the Monster.
1
|
|
No need to define properties anymore. We can create a monster in a single line.
Access Modifiers
There are three access modifiers in TypeScript; private, protected, and default (public). The private only allows to its class. The protected allows its sub-classes.
1 2 3 4 5 6 7 8 9 |
|
I put the print method into the interface of Properties to make it a must-defined method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
I added a prefix of _ (underscore) to name, feet, and hasPaws to make them different from getters and setters. I also made them private.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Same thing for the Monster.
Closing
You can get the source code at https://github.com/aristorinjuang/ts-oop. Try to check out my references. They are good resources from official TypeScript. I didn't try static property or method, abstract class, decorator, overloads, or more. But I hope this article can give you a basic understanding of OOP in TypeScript.
References
Related Articles
- Create a Private NodeJS Module with TypeScript
- Design Patterns with Go and TypeScript
- React Context with TypeScript
- Convert Any Images to JPEG and WebP, Compress, and Resize on the Client Side