Interface Methods in TypeScript
Object interfaces can also add object methods. Let's add a method to our interface that should greet the user. To do this, we must specify the method name, parameter types, and return type:
interface IUser {
name: string;
age: number;
greet(text: string): string;
}
Let's implement our method in an object:
let user: IUser = {
name: 'john',
age: 30,
greet(text: string): string {
return text + ', ' + this.name
}
}
Let's call our method:
console.log(user.greet('hello')); // 'hello, john'
For the IUser interface, implement a method to check the age. If the user's age is less than 18, display a message that access is denied.
Create an interface IMath with properties num1 and num2, and a method getSum that will sum both numbers.