In this video, I want to go over two more pieces of syntax around interfaces, so to get started,
I'm going to go down to the vehicle function inside of here.
You'll notice that our implementation now only accesses one property on the vehicle argument.
The only thing 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 called 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 type script behind the scenes, does a quick check for us.
It says, does this object right here satisfied the vehicle interface?
So TypeScript is going to 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 little 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 from 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 a print vehicles, there is no more vehicle types.
I'm going to replace that with reportable.
Now, just to 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 down 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 function really 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.
Reports about item, and maybe that makes sense.
And then inside our rather than Collins 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 going to come back to the next video and we're going to discuss why this was a very significant
factor by adding in just a little bit more code.
So quick pause and we'll see you in just a minute.