Spread the love

Interface in angular or in any programming languages is pattern which bind component or class to use same implementation across the modules. In this article we will cover the use of interface in angular with example. In Angular interface can be used on service, factory, component or any type of class so we can assign a same architecture pattern to all classes.

As we know angular heavily use interface for a variable, array, objects and class so in this example we will use it in a component with array of object.

I will take you through with a simple example of employee name, id and status so that you can use interface in any version of angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application.

What is Interface in Angular?

Interface in angular or any language its a architectural pattern which sets some rules for a class, method, property or object to be followed.

Let’s understand it quickly with an example

Suppose we have a object which have three properties name, id and status. name must be string, id must be a integer and status must be a enum. So while working in a team then multiple developers should follow this rule of object data types if we don’t apply the interface then any of the developer can assign any type of data type then it will produce run time errors so to bound these things we use interfaces.

How to make interface ?

In angular we can create interface anywhere and then we can import it in component, service or any classs as follow

export interface Employee {

    id: number;

    name: string;
    status: string


Here we created a interface using keyword interface then to use it globally we exported it and Employee is the name of interface that we can import and use in any class or component.

id is type of number , status and name is string so when we use it we can only define this type of data so let’s use it.

How to use interface in angular component ?

To use the interface in component we first need to import it as follow

import { Component } from '@angular/core';
import { Employee } from "./employee-struct.ts" 
  selector: 'my-app',
  templateUrl: './employee.component.html',
  styleUrls: [ './employee.component.css' ]
export class EmployeeComponent  {
  name = 'Employee component';
  employes: Employee[] = [
    {id: 1, name: "james",status:"active"},
    {id: 2, name: "rock",status:"active"},
    {id: 3, name: "denis",status:"active"},

As you can see here we imported the employee interface then used it as employes: Employee[] = []

Also Read : Create custom form control in angular 12 / 13

Leave a Reply