Interface

Shivani Nalawade
2 min readApr 7, 2020

--

“In this section, I will tell you what is interface and the example of interface ”

  • Typescript provides three ways for custom types : Interfaces, Enums, and Classes. In this article we’ll focus on interface, how to create and use them.

Definition: A class is a blueprint from which we can create objects that share same properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.

  • Interfaces are capable of describing the wide range of shapes that JavaScript objects can take. In addition to describing an object with properties, interfaces are also capable of describing function types.
  • An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface.when we implement interface we are very much sure ,that the method in the interface is implemented in the class.

Interface:

Properties:

  1. Interface must be declared with the key word ‘interface
  2. We cannot create an instance of interface. For implementing interface we need to use Implements keyword.
  3. In an interface, we can only declare properties and methods.
  4. An interface type objects cannot declare any new methods or variables.
  5. Interface enforces the variables and methods which have to be present in an object.

Syntax:

interface interface_name{

//Declaration part

}

Example: Interface

interface IEmployee {
empCode: number;
empName: string;
getSalary: (number) => number; // arrow function
getManagerName(number): string;
}

In the above example, the IEmployee interface includes two properties empCode and empName. It also includes a method declaration getSalary using an arrow function which includes one number parameter and a number return type. The getManagerName method is declared using a normal function. This means that any object of type IEmployee must define the two properties and two methods.

To better understand this concept please do watch the below video.It will resolve your all doubts:

--

--

No responses yet