Web Technologies - TypeScript

Back to Course

Lesson Description

Lession - #1448 TypeScript Interface

An interface is a syntactical contract that an entity should conform to. In other words, an interface defines the syntax that any entity must adhere to. Interfaces define properties, methods, and events, which are the members of the interface.

Why do we use interface in TypeScript?
Interfaces in TypeScript have two usage scenarios you can create a contract that classes must follow, similar as the members that those classes must apply, and you can also represent types in your application, just like the normal type declaration.

Declaring Interfaces
The interface keyword is used to declare an interface. Here's the syntax to declare an interface −



Example Interface and Objects

var customer = {
    firstName: "Tom",
    lastName: "Hanks",
    sayHi: function (>
{ return "Hi there"; } }; console.log("Customer Object ">
; console.log(customer.firstName>
; console.log(customer.lastName>
; console.log(customer.sayHi(>
; var employee = { firstName: "Jim", lastName: "Blakes", sayHi: function (>
{ return "Hello!!!"; } }; console.log("Employee Object ">
; console.log(employee.firstName>
; console.log(employee.lastName>

Online compiler