- Interfaces define properties, methods and events, which are the members of the interface.
- Interfaces restricted to only the declaration of all the members.
- The deriving class will have the defining and implementation of the members.
Syntax
1 2 |
interface interface_name { } |
Example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Interface declaration interface Employee { firstName: string, lastName: string, greet: () => string } // object of type Employee var employeeObj: Employee = { firstName: "shri", lastName: "kumar", greet: (): string => { return "Hi there !" } } console.log("Customer Object ") console.log(employeeObj.firstName) // output: shri console.log(employeeObj.lastName) // output: kumar console.log(employeeObj.greet()) // output: Hi there ! |
above example, employee is the interface, and employeeObj object is type of Employee.