In the last video, he wrote out a little bit of code, but it's got one little issue.
We've got a really long type annotation 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 annotation right here several times
over and it 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 up 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, any time 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 boolean or so on.
So I cannot 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'll 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 riding out this really long form type annotation right here, I can delete that
full annotation and replace it with just a vehicle like so.
So what are we saying here by putting this code in, we are now saying that in order to call it a 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 of 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 air and no warning
or anything like that.
So as soon as we added in the statement, TypeScript did 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.
So 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 in old Civic into the print vehicle function.
And if I hover over that air right there, it's going to tell me very plainly down towards the bottom
of this air right 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 an interface
is.
So let's take a quick pause right here.
We're going to come back to the next video and start to expand on this example just a little bit more.