In this lecture, we're going to look at how we can use interfaces to make our objects more readable.
The syntax for annotating an object kind of looks ugly and messy.
I'm not a huge fan of it.
We have two objects being annotated.
Let's clean this up by using an interface.
An interface is a typescript specific feature for creating types for objects.
It's an alternative syntax to what we're currently doing.
Let's explore how an interface is created.
Hopefully, once we've written an interface, it'll become clear why you may want to use them above
the objects.
We will type the interface keyword.
The interfaith keyword allows us to create a custom type foreign object instead of directly adding the
types on the variable, we can extract the types to an interface.
After typing the interface, we need to provide a name while not required.
Most developers like to use Pascal casing for their interface names.
In addition, some developers will add a capital i at the beginning of their name, which is short for
interface.
It helps other developers identify this as an interface.
For this example, we will set the name to AI accounts.
Next, we can add the tapes for the object.
We've already done so below.
We will cut and paste the object annotation from the account variable.
A couple of things worth mentioning.
The interface cannot hold values, interfaces are not a replacement for objects.
They're a feature for helping us outsource the typing of an object.
Another thing worth mentioning is that they don't get compiled into JavaScript when we compile our code.
The interface will be completely absent from our JavaScript file.
Similar to how primitive types are removed from variables, interfaces get removed, too.
Let's make our interface more interesting.
Objects can have methods, so let's add a method.
The method will be called deposit.
It's completely acceptable to add methods, but there's one thing to keep in mind.
We can't have business logic in our method.
It's up to the object to implement the business logic.
However we can and the parameters and return type, for this example, we won't have parameters.
The return type will be void.
The avoid type is a special type for functions, sometimes you may not want to return a value from your
function if that's the case.
You can annotate a function with the void type, which means no return value.
One last thing?
We will make this method optional.
Let's use our interface.
We can apply the interface after the name of the variable instead of adding an object.
We can pass it in the name of the interface.
We have another variable called accounts, which is basically an array of accounts.
It would make sense to apply the interface here, too.
We will replace the object with the name of the interface.
The square brackets can remain.
This syntax will tell TypeScript.
We will have an array of accounts by using an interface.
Our code looks so much cleaner.
I recommend using interfaces whenever working with objects in the next lecture.
We'll look at an alternative syntax for creating objects.