Interface in TypeScript?

What is an Interface in TypeScript?

An interface in TypeScript is similar to other object oriented programming languages interfaces.
An interface is a way to define a contract on a function with respect to the arguments.

Stayed Informed – Learn Angular 2

In the below example, I am using an interface that describes objects that have a “name”, “age” and “address” fields and the following code defines an interface and a function that takes a parameter that adheres to that interface.

//USER INTERFACE 
interface User {
    name: string;
    age: number;
    address: string
}

//FUNCTION USING USER INTERFACE 
let userInfo = function(user: User) {
   let info = "Hello, " + user.name + " Your Age is - " + user.age + " and Address is -" + user.address;

   return info;
}

//USER INFO JSON OBJECT
let info = {
    name: "Anil",
    age: 30,
    address: "Noida, India."
};

//RESULT
console.log(userInfo(info));

I hope you are enjoying with this post! Please share with you friends. Thank you!!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.