In this video I want to go over two more pieces of syntax around interfaces so to get started.
I'm gonna go down to the print vehicle function inside of here you'll notice that our implementation
now only accesses one property on the vehicle argument.
The only thing where we are referencing is these summary property.
So I got a question for you.
We are saying that the argument must be a vehicle and must satisfy this interface is the terminology
we would use.
And we've said earlier that the vehicle must have a name a year broken and the summary function.
However our function down here is now only accessing the summary property.
So here's the question is it important for us to say that a vehicle must have these properties available.
I don't think it really is.
So what happens if I delete those additional properties.
I can delete them.
They go away.
And now you'll notice I do not see any errors inside my file.
Why is that.
Well when I call print vehicle right here we are saying that in order to be passed into this function
you must satisfy the vehicle interface.
So we pass an old Civic and typescript behind the scenes does a quick check for us it says.
Does this object right here satisfied the vehicle interface so typescript is gonna go back to the vehicle
interface definition and it says in order to be considered to be a vehicle you must have a function
called summary that returns a string.
It then looks at the old Civic object and it says OK does this object have a summary property that is
a function that returns a string.
Yes it does.
And that's the only question that gets asked.
It doesn't matter if we have additional properties on old civic.
Doesn't matter when typescript tries to decide if old Civic is a vehicle or not.
It's not going to say Oh you've got these extra properties sorry you can't be a vehicle.
It's only going to check to see if it has this list of properties right here.
If it has that list awesome it is qualified to be a vehicle so the code we have here still works.
But we've got a much more condensed interface definition.
So here's the important thing here.
Something I want to point out as like kind of step two of this old refactor now that our interface only
has a single function definition in it.
Do you think the name vehicle is really appropriate here.
Like is calling an object that has a summary property that is a function that returns a string.
Does it really seem appropriate to call that a vehicle.
I would say maybe it doesn't.
Maybe a more appropriate interface or a name for this interface would now be something like.
I don't know reportable or something like that.
So now we're saying that in order to be considered the type reportable you must have a function called
summary that returns a string.
And so now to fix up our file here down here inside of print vehicles there is no more vehicle type
song and replace that with reportable now just be clear the word reportable.
I'm kind of meaning to say something that is reportable has a summary function that returns a string.
So this is like something that can be reported all right.
So notice how our definition for the interface is now a lot shorter and a little bit more generic than
it was before it's now back down inside the print vehicle interface or function out here I want to just
point out one more thing here.
Notice how we've got a function called Print vehicle.
It takes an argument called vehicle and it refers to that function or that argument inside the function.
But once again I got a question for you.
Does anything about this function now really seem like it has anything to do with vehicles.
I don't think it really does.
The only thing that this functionally does is taken something that can be reported on and then console
log a summary about that thing.
So maybe we should update some of the terminology inside this function maybe instead of an argument
called vehicle we call this something that's called like I don't know report I don't like item.
Maybe that makes sense and then inside are rather than coins thing print vehicle.
I want to call it maybe something a little bit more appropriate like print summary or something like
that.
I'm going to update the reference down here as well to be simply print Summary Ok.
So all we've really done here is kind of pointed out how our interface no longer really has anything
to do specifically with vehicles.
And so I just decided to rename it into something a little bit more generic and I did the same thing
with the function down here so let's take a quick pause right here.
We're gonna come back the next video and we're going to discuss why this was a very significant refactor
by adding in just a little bit more code so quick pause and I'll see you in just a minute.