Explain the Concept of Interfaces in TypeScript ?
Last Updated :
20 Feb, 2024
The interfaces in TypeScript are used to define a particular type of object to which the objects must adhere. They act like a blue-print for the objects and define the structure of the objects. They are mainly used to type the keys of the object by specifying their types and then use the declared interface to type the object in the code.
Syntax:
interface interface_name{
key1: type1;
key2: type2;
}
const object_name: interface_name = {
// Object values
}
Example: The below code explains the use of the interface to type an object in TypeScript.
Javascript
interface myInterface {
name: string;
desc: string;
est: number;
}
const myObj: myInterface = {
name: "GeeksforGeeks" ,
desc: "A Computer Science Portal" ,
est: 2009
}
console.log( "Welcome to " , myObj.name, ", " ,
myObj.desc, ", " , "Established in: " ,
myObj.est);
|
Output:
Welcome to GeeksforGeeks, A Computer Science Portal, Establishe in: 2009.
Share your thoughts in the comments
Please Login to comment...