Now if we come back to this interface here the question is why do we need this wouldn't we have the
exact same thing if we would at a custom type here and there.
The difference is just that we have to have a equals sign and then we're saying the person type is an
object which looks like this.
And indeed if we saved added compiles without errors so we could replace our interface with such a type.
And it works as before.
So why do we have an interface then.
Well an interface and a custom type are not exactly the same.
Whilst often you can use them interchangeably and you can use the interface instead of a custom type
or the other way around.
There also are some differences.
Well one major difference is that interfaces can only be used to describe the structure of an object.
You can use type for that as well but instead of a custom type you can also store other things like
union types and so on what we did earlier in the course that sounds like type is therefore more flexible.
But the other side of the coin is that interface is clearer when you define something as an interface.
It's super clear that you want to define the structure of an object with that.
And indeed when it comes to defining object types you more often see interfaces out there in the wild.
Then you see custom types.
Now another thing you can do with interfaces but you would also be able to do with custom types is you
can implement an interface in a class.
What do I mean by that.
The reason why you often work with interfaces is dead.
Interface can be used as a contract.
A class can implement and a class then has to adhere to.
What do you mean with that.
Let's say our interface is not a person interface with a name in the age but instead we name it.
Greed double notice might look like a strange name but in the end this just says any object dad should
be treated as readable has to be object with a name and with a creat method.
So now we could say this here this user one variable should hold a reasonable object.
And of course now we get an error because we have more than just name and greet.
But we could use this interface to share it amongst possibly multiple classes.
We have to ensure that every class that adheres to this interface has to have a name property and has
to have a great method.
So here we could now add a person class the name is available again because I renamed the interface
and now tell typescript that this class should basically adhere to this interface it should implement
this interface it should follow that contract set up by this interface.
We do it by adding the implements keyword offered a class name and then the name of our interface readable.
And you can implement more than one interface.
That's a difference compared to inheritance you can inherit only from one class.
You can implement multiple interfaces by simply separating them with a comma.
So there we could have another interface if we had one here.
I only have one though.
And now the implication of this is that we get an error here because our class incorrectly implements
the interface.
It does not have the name property does not have the Greek method.
So now we have to set this year in our class maybe also add a constructor where we get a value for the
name and then set it up like this.
And where we also add our agreed method where we then might have this logic from now there.
Now in our class we can't have more than that.
We can't have more fields.
We can have more methods.
We can also extend this class.
We can work with this class as we always can but we're forced to implement this method correctly to
have this name property because we're implementing this interface and ever interfaces are often used
to share functionality amongst different classes not regarding their concrete implementation.
You can't have implementation or values instead of interfaces.
But regarding the structure regarding the features a class should have.
It's a bit like working with abstract class to stay up for the difference being that an interface has
no implementation details at all whereas abstract classes can be a mixture of you have to overwrite
this parts.
And I have a concrete implementation parts.
That's an important difference between interfaces and abstract classes.
Now with that however we have our Person class here and now here User 1 can be created by using new
person and feeding in the name for example.
And now you see it's OK that we have this eight year end user one because we build user one based on
person person implements treatable and you can add more than treatable ones in a class but you have
to satisfy the conditions set up by the interface.
And if for now if we save everything we have a valid code.
And if we log our user one down there with console log we see that it is a normal object with an age
and a name in this case.
But the key takeaway is that it is based on a class which implements the interface and you can then
all use the interface as a type of course we could have set this to type person.
But as you see using type Greta here all the works because the person object which we store in the user
one class in the end is based on the readable interface it implements this.
So that's another powerful feature you can use an interface as a type on some constant or variable which
will then actually store and never a class of a number type which in turn is based on the interface
type because it implements it.
So that's interfaces in a nutshell.
Lot of powerful features included.