Javatpoint Logo
Javatpoint Logo

TypeScript Interface

An Interface is a structure which acts as a contract in our application. It defines the syntax for classes to follow, means a class that implements an interface is bound to implement all its members. It cannot be instantiated but 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 just declares the methods and fields. It cannot be used to build anything. An interface is inherited by a class, and the class which implements the interface has to define all members of the interface.

When the Interface is compiled from Typescript compiler to JavaScript, then in JavaScript file, interfaces disappear. Thus, their only purpose is to help in the development stage.

Interface Declaration

The syntax to declare an interface is given below.

  • an interface is a keyword which is used to declare a TypeScript Interface.
  • interface_name is the name of the interface.
  • an interface body contains variables and methods declarations.

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 which has one argument type which is the type of interface OS.

Now, we can compile the TS file to the JS and see the output.

Output:

Android O has Java language.

Use of Interface

Interfaces are mainly used for validating the specific structure of properties, objects passed as parameters, and objects returned from functions.

Interface Inheritance

An interface can be inherited from 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.

We can understand the interface inheritance with the following example:

Syntax

Example

Output:

Name: Abhishek
Employee Code: 43

Let's take another example which understands the multiple interface inheritance.

Example

Output:

Name: Abhishek
Employee Code: 43

Array Type Interface

We can also use interfaces to describe the array type. We can understand this by the below example.

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:

My age is: 18

Interface in a class

TypeScript also has the ability to use interface in a class. The interface is being implemented by a class using implements keyword. We can understand this by 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. This interface is implemented by Employee class by using implements keyword. After implementing an interface, we must declare those 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 necessary parameters otherwise it throws an error at compile time.

Output:

Name of Person: Abhishek Mishra
Age: 25

Difference between interface and inheritance

SN Interface Inheritance
1. An Interface is a structure which acts as a contract in our application. It defines the required functions that must be implemented by a class to meet that contract. Inheritance is object-oriented programming that allows similar objects to inherit functionality and data from each other.
2. In an interface, we can only declare properties and methods. In inheritance, we can use a superclass to declares and defines variables and methods.
3. An interface type objects cannot declare any new methods or variables. In this, we can declare and define its own variables and methods of a subclass that inherits a superclass.
4. Interface enforces the variables and methods that have to be present in an object. A subclass extends the capability of a superclass to suit the custom needs.
5. Interface are classes that contain body-less structure (abstract or virtual functions). So, we have to derive the interface and then implement all of the functions in the subclass. Inheritance is the process where one subclass acquires the properties of its superclass.




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA