| Start |
|---|
| Extending Interface Suppose we have an interface: And we want to create more specific interface that has the same properties of the person, we can do it using the In addition it is possible to extend multiple interfaces. |
| Class Interface Declare Here we create a class that implements the interface. The example shows how to create an interface |
| Add functions or properties to an existing interface Let's suppose we have a reference to the The compiler will merge all declarations with the same name into one - see declaration merging (opens new window) for more details. |
| Generic Interfaces Like classes, interfaces can receive polymorphic parameters (aka Generics) too. Declaring Generic Parameters on InterfacesHere, you can see that our two interfaces take some generic parameters, T and U. Implementing Generic InterfacesWe will create a simple class in order to implements the interface IEvents. Let's create some instances of our State class. In our example, the Here our Our As you can see, generic interfaces can be a very useful tool for statically typed code. |
| Using Interfaces for Polymorphism The primary reason to use interfaces to achieve polymorphism and provide developers to implement on their own way in future by implementing interface's methods. Suppose we have an interface and three classes: This is connector interface. Now we will implement that for Wifi communication. Here we have developed our concrete class named Now we are creating our
See that Wifi and Bluetooth have its own implementation. There own different way to connect. However, hence both have implemented Type This is called Duck typing (opens new window). if at |
| Using Interfaces to Enforce Types One of the core benefits of Typescript is that it enforces data types of values that you are passing around your code to help prevent mistakes. Let's say you're making a pet dating application. You have this simple function that checks if two pets are compatible with each other... This is completely functional code, but it would be far too easy for someone, especially other people working on this application who didn't write this function, to be unaware that they are supposed to pass it objects with 'species' and 'age' properties. They may mistakenly try One way we can prevent this from happening is to specify the properties we want on the pet parameters: In this case, Typescript will make sure everything passed to the function has 'species' and 'age' properties (it is okay if they have additional properties), but this is a bit of an unwieldy solution, even with only two properties specified. With interfaces, there is a better way! First we define our interface: Now all we have to do is specify the type of our parameters as our new interface, like so... ... and Typescript will make sure that the parameters passed to our function contain the properties specified in the Pet interface! |
| Implicit Implementation And Object Shape TypeScript supports interfaces, but the compiler outputs JavaScript, which doesn't. Therefore, interfaces are effectively lost in the compile step. This is why type checking on interfaces relies on the shape of the object - meaning whether the object supports the fields and functions on the interface - and not on whether the interface is actually implemented or not. So even if Syntax
RemarksInterfaces vs Type AliasesInterfaces are good for specifying the shape of an object, eg for a person object you could specify However what if you want to represent, say, the way a person is stored in an SQL database? Seeing as each DB entry consists of a row of shape This is an occasion where types come in useful. Instead of specifying in every function that accepts a row parameter Official interface documentationhttps://www.typescriptlang.org/docs/handbook/interfaces.html (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference