In the last video we wrote out a little bit of code but it's got one little issue.
We've got a really long type indentation on this function right here.
I think you can easily imagine that if we had several other functions inside this file that all expected
to be called with old Civic we would have to repeat this type notation right here several times over
and would be a lot of duplicate code.
So to fix this we're going to create an interface inside this file.
I'm going to go to the very top and I'm going to define an interface by writing out the interface keyword
and then a name for the new type that I am creating.
Remember anytime we create an interface we are creating a new type a type in the same way that a number
is a type or a string or a boolean or so on song you create an interface called vehicle whenever we
create an interface we're always going to use a capital letter on the leading edge of the word.
You also know some using kind of a generic term right here.
So I'm not creating an interface called like civic or an interface called old Civic or new civic or
anything like that.
Instead I'm creating a very generic name right here to describe what different properties a vehicle
might have.
So then inside these curly braces I'm going to list out the different property names that I expect an
object that is a vehicle to have and they're different types.
So in order to be a vehicle you must have a name property that is a string you must have a year property
that is a number and you must have a broken property that is a boolean like so so once again we have
now created a new type inside of our application called vehicle and in order to be a vehicle you have
to have these properties and they must have these different types so we can now think of vehicle right
here as being kind of like a variable of sorts that refers to a type.
So rather than writing out this really long form type notation right here I can delete that full annotation
and replace it with just vehicle like so so what are we saying here by putting this code in.
We are now saying that in order to call the print vehicle function you must pass in an object that meets
the specifications provided by the vehicle interface.
In other words to call this function you must provide an object that has a name that is a string a year
that is a number and broken that is a boolean the old Civic object satisfies each those requirements.
It has a name that's a string year that's a number and broken that is a boolean.
So we are allowed to pass an old Civic to print vehicle.
You'll notice that down here where we actually call a print vehicle there is no error and no warning
or anything like that so as soon as we added in the statement typescript scripted a little bit of checking
for us a little process went on behind the scenes typescript took a look at the argument we provided
and it said OK we need to look at that old Civic object and make sure that it satisfies this interface.
We need to make sure that old Civic has all the appropriate property names and types and so behind the
scenes typescript loop through all these different properties and made sure that old Civic had each
of those different property names and the appropriate value for each one if I change any of these types
right here and assign a different type let's let's say instead of broken I'm going to put in like just
a one like so as soon as I do that you'll notice we get an error message down here typescript is telling
us that in order to pass an old Civic we have to have a broken property that is a boolean but we have
a broken property that is a number and that's why we're seeing an error.
So if I change that one back to true everything works again.
Likewise if I misspell any of the property names inside the subject like let's say is broken instead.
Now our object can no longer be considered to be a vehicle remember to be a vehicle we have to have
a broken property but I provide a property that is is broken because those property names are different
typescript will not allow me to pass an old Civic into the print vehicle function and if I hover over
that error right there it's going to tell me very plainly down towards the bottom of this area here.
It says Property broken is not present inside the object I provided but it is required to be a vehicle
so once again if I change is broken back over to broken everything works again OK.
So this right here is some of the like very basic interaction or definition of what it interfaces.
Let's take a quick pause right here going to come back the next video and start to expand on this example
just a little bit more.