TypeScript InterfaceAn Interface is a structure which acts as a contract in our application. It defines the syntax for classes to follow, means a class which implements an interface is bound to implement all its members. We cannot instantiate the interface, but it can be referenced by the class object that implements it. The TypeScript compiler uses interface for type-checking (also known as "duck typing" or "structural subtyping") whether the object has a specific structure or not. The interface contains only the declaration of the methods and fields, but not the implementation. We cannot use it to build anything. It is inherited by a class, and the class which implements interface defines all members of the interface. When the Typescript compiler compiles it into JavaScript, then the interface will be disappeared from the JavaScript file. Thus, its purpose is to help in the development stage only. Interface DeclarationWe can declare an interface as below.
Example In the above example, we have created an interface OS with properties name and language of string type. Next, we have defined a function, with one argument, which is the type of interface OS. Now, compile the TS file into the JS which looks like the below output. Output: Use of InterfaceWe can use the interface for the following things:
Interface InheritanceWe can inherit the interface from the other interfaces. In other words, Typescript allows an interface to be inherited from zero or more base types. The base type can be a class or interface. We can use the "extends" keyword to implement inheritance among interfaces. The following example helps us to understand the interface inheritance more clearly. Syntax Example Output: Let us take the above example, which helps us to understand the multiple interface inheritance. Example Output: Array Type InterfaceWe can also use interfaces to describe the array type. The following example helps us to understand the Array Type Interface. Example In the above example, we have declared nameArray that returns string and ageArray that returns number. The type of index in the array is always number so that we can retrieve array elements with the use of its index position in the array. Output: Interface in a classTypeScript also allows us to use the interface in a class. A class implements the interface by using the implements keyword. We can understand it with the below example. Example In the above example, we have declared Person interface with firstName, lastName as property and FullName and GetAge as method/function. The Employee class implements this interface by using the implements keyword. After implementing an interface, we must declare the properties and methods in the class. If we do not implement those properties and methods, it throws a compile-time error. We have also declared a constructor in the class. So when we instantiate the class, we need to pass the necessary parameters otherwise it throws an error at compile time. Output: Difference between interface and inheritance
Next TopicTypeScript Namespaces
|