This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel
In today's article, I want to talk about Polymorphism in TypeScript, which is one of four pillars of object-orientated programming.
What is Polymorphism?
Polymorphism is the ability to create a class that has more than one form. Or in other words, classes have the same methods but different implementations.
Example
So, before explaining how polymorphism works, I better show you, how it does not work by an example.
Imagine we have two animal classes, one class is called Dog, the other class is called Cat. Both classes should have the same properties and methods.
And that works fine, but actually, that is not the way to go. As I said, both classes have the exact same properties and methods, so it makes sense to inherit this from a parent class.
Let’s have a look at a better way:
The results of the makeSound methods are exactly the same, but the approach is a little bit different.
GitHub - hellokvn/medium-polymorphism-in-typescript
Thanks for reading my article about Polypmorphism in TypeScript. I hope, I could refresh your knowledge with this quick example.
Cheers
Use any component in all your projects
Up until now, you used to build features hidden inside larger projects.
But what if you were to develop independent features first, and then easily compose and manage them in many applications? Your development will become faster, more consistent, and more scalable every day. Create a component once, and truly use it anywhere to build anything.
OSS Tools like Bit offer a powerful developer experience for building independent components and composing them to applications. You can start small with a nice project, some shared components, or even trying out Micro Frontends. Give it a try →
Learn more
- Building a React Component Library — The Right Way
- 7 Tools for Faster Frontend Development in 2022
- Microservices are Dead — Long Live Miniservices
Polymorphism in TypeScript (OOP) was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel
Kevin Vogel | Sciencx (2022-02-18T08:23:56+00:00) Polymorphism in TypeScript (OOP). Retrieved from https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.