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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book