Quick Read on Interfaces in TypeScript in 2 mins

Learn about Interfaces in Typescript and how to implement TypeScript in 2021

In this tutorial, we will discuss how to implement the interface in TypeScript. Earlier, we discussed the variable, array, functions in TypeScript. If you would like to revisit, you can access it from the menu table on the left or the menu icon if you’re using it from mobile. 

The interface is a structure that characterizes the agreement in your application. It describes the language structure for classes to follow. Classes from an interface must follow the structure given by their interface.

In TypeScript, the syntax to create an interface is as follows.

Interfaces in TypeScript
Interfaces in TypeScript


Now we will discuss how to implement an Interface inside a function. For this, we first need to create an interface, followed by a function and then we need to print the interface using the function.

Step to create the interface.

interface Students {
    firstName: string;
    lastName: string;
} 

Step to create a function. 

function greetings1(students: Students){
     return "Hello, " + students.firstName + " " + students.lastName; 
}

Now the function is created. The next step is to input the person name dynamically and print it. The following code will do the task at hand. 

let person = {firstName: "John", lastName: "Doe"};
console.log(greetings1(person));

Now that you have learnt about Interfaces in TypeScript. You can also check our tutorial Getting Started with TypeScript. Check our Angular with Angular Material Training from Ampersand Academy


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *