In this video we're going to start writing out some code to get a better understanding of how interfaces
work.
You'll notice I'm back inside my code editor and inside my root project directory I'm going to make
a new file called interfaces dot T.S. All right.
So inside of here we're not going to write out an interface just yet.
Instead I first want to define an object that represents a car rather than going to write a function
that kind of operates on that object and we'll start to introduce interfaces so to get started.
I'm going to make a variable called old Civic like so casino a Civic is a type of car or like a vehicle
of sorts.
So it's a Honda Civic.
By saying old Civic I just mean to indicate that we are going to make an object that represents a civic.
That was made many years ago.
So inside this object I'm going to add a couple of different properties to describe exactly some things
about the civic.
So I gonna give it a model property actually.
Let's give it a name instead.
That's just all a bit more clear.
So this car is going to have a name of civic.
I'll then give it a year that it is built of 2000 and then a broken property to describe whether or
not this vehicle still runs.
I'll say it's broken.
True now that we've put that object together.
I want to try creating a function that's going to take in this object and just print out some different
properties about it.
So I want to define a function called Print vehicle
then I'm going to add in a single argument.
So we want to call this function with a car.
I'm going to refer to this argument has How about just like a vehicle that makes sense.
And then remember we need to add in a type annotation to describe exactly what properties or what type
vehicle is right now if I hover over a vehicle it says that it is of type any.
Remember we want to avoid any types inside our application.
Now we saw how to add in a annotation for an object inside of a function just a moment ago.
So we're gonna put in the set of curly braces or to me a colon right there and then a set of curly braces.
And then we'll list out all the different properties that we expect this argument to have.
And they're different types.
So I expect a vehicle to have a name that is a string remember inside this type annotation to separate
different properties will place a set of a semicolon like so I then expect a vehicle to have a year
that is a number I'm gonna zoom out for a second so you can see this whole line and I expect a vehicle
to have a broken flag that is going to be a boolean finally I'm going to add in a type invitation to
the return of this function.
So right after the argument list I'll put in a colon.
I don't expect this function to return anything so I'm going to give it a type annotation of void like
so remember void means our function returns nothing.
All right.
I get zoom back in.
It's now inside of here we can do our actual implementation.
So I'll just do a couple of console logs using properties from the vehicle I can use.
Yes 2015 template string so I can use some back tricks here.
I'll do some things like let's say How about name and I'll do vehicle name.
I'll do another little statement here I'll do something in like Year the school year and then one more
for the broken flag as well.
So broken no typo there.
There we go.
V Hickel dot broken goal.
So now we should be able to call print vehicle and pass an old Civic old civic structure is a name that
is string year that is a number and broken that is a boolean.
And so that meets the type invitation we just put on that argument right there.
So we should be able to pass an old Civic and not have any issue.
So I'm going to take print vehicle and pass an old Civic and everything looks good to me.
Perfect.
All right.
Now I say perfect but maybe not quite perfect.
There's one little issue inside this file right now.
I bet you can guess what it is this type annotation right here is really long.
It's kind of hard to read and if we had any more properties on this object that type invitation would
become even harder to read.
In addition if we added in more functions that expected to be we expected to call with old civic.
We would then be duplicating this type annotation for each of those different functions.
And so we would have this really long annotation right here duplicated several times throughout this
file.
So as you might imagine.
Yeah that's probably not the best.
So let's take a quick pause right here.
We're going to come back the next video and figure out how we can improve this file by using an interface.