Classes are ideal to make new instances of a group of properties and methods.
Now that we have defined the
class Vehicle we always know which properties it has. When we make an instance of it, it has for sure those properties.
Now it is a given that the car variable has properties: type, length, width, height, and wheels. But we don’t know the values of it. When you log this to the console you find our that they are equal to the class we defined.
When asking for the square centimeter it will be 0.
To change the values, we need to give them when we create the instance or we do it afterward.
If you log the variable to the console, you will see that the values are changed.
How do we prevent undefined errors with this?
Well, when defining a class you give it properties and or methods. So it’s not gonna be a guess if the property you need somewhere else, is gonna be there because it will be there!
Let’s make it more predictable in TypeScript 🙌
When you define a
function and give it a parameter
car. But you want to make sure the
car is based on the class
Vehicle and have all the properties you expect.
We can guarantee that TypeScript is gonna give you an error if you give it a regular Object like below.
The error will look like the screenshot below.
This is the reason why I love to work with TypeScript. It helps you prevent all kinds of error’s while developing instead of finding them as run-time errors in your browser.
Thanks for reading this far 😉
I hope this short guide will help you prevent those annoying errors and gonna push you forwards to use classes.
Before you make any object in your code, think twice and consider making a class for it 😉.
Read more of my Medium Article’s:
Dive into GraphQL w/ Apollo Client by building a SpaceX 🚀 app in Angular 8
_I know that this sounds odder than it is, but using GraphQL in an Angular 8 application with Apollo Client feels like…_itnext.io